QProcess 没有调用完成?

QProcess not invoking finished?

我尝试实施 subprocess Popen blocking PyQt GUI 的建议,但似乎永远不会调用 onFinished 函数。

class MyApp(QtWidgets.QMainWindow, Ui_MainWindow):

[...]

    def Run_PnL_Script(self):
        self.Run_PnL.setEnabled(False)
        self.Run_PnL.setStyleSheet("background-color: #D3D3D3; color: #FFFFFF")
        self.PnL_Status.setText("Running...")
        process = QtCore.QProcess(self)
        process.finished.connect(self.onFinished)
        process.startDetached("cmd.exe", ["/c", "K:\Market Risk\Risk App\Batches\RTest.bat"])

    def onFinished(self, exitCode, exitStatus):
        self.PnL_Status.setText("Complete.")
        self.Run_PnL.setEnabled(True)
        self.Run_PnL.setStyleSheet("background-color: #4582EC; color: #FFFFFF")

[...]

在此先感谢您的帮助。

您有 2 个错误:

  • 当你执行完cmd.exe中的命令并不意味着你关闭cmd.exe,所以你必须使用.bat末尾的EXIT命令关闭它。

RTest.bat

:: Another commands
EXIT
  • 不要使用startDetached() since this is a static method that creates an internal QProcess different from the "process" variable, you must use the start()方法。
# ...
process = QtCore.QProcess(self)
process.finished.connect(self.onFinished)
process.start("cmd.exe", ["/c", "K:\Market Risk\Risk App\Batches\RTest.bat"])