1+ from sys import getdefaultencoding
12from typing import Optional , Sequence
23
34from PySide6 import QtCore
@@ -21,7 +22,7 @@ def __init__(self, parent: Optional[QtCore.QObject] = None) -> None:
2122
2223 def start_process (self , program : str , arguments : Sequence [str ]) -> None :
2324 """
24- 启动子进程 \n
25+ 使用给定参数启动指定子进程 \n
2526 :param program: 子进程命令
2627 :param arguments: 子进程参数
2728 :return: None
@@ -30,18 +31,27 @@ def start_process(self, program: str, arguments: Sequence[str]) -> None:
3031 if self .process is None : # 防止在子进程运行结束前重复启动
3132 self .process = QtCore .QProcess ()
3233
34+ self .process .stateChanged .connect (self ._handle_state ) # type: ignore
3335 self .process .readyReadStandardOutput .connect (self ._handle_stdout ) # type: ignore
3436 self .process .readyReadStandardError .connect (self ._handle_stderr ) # type: ignore
35- self .process .stateChanged .connect (self ._handle_state ) # type: ignore
37+ self .process .started .connect (self ._process_started ) # type: ignore
3638 self .process .finished .connect (self ._process_finished ) # type: ignore
3739
3840 self .process .start (program , arguments )
3941
42+ def _process_started (self ) -> None :
43+ """
44+ 处理子进程的槽 \n
45+ :return: None
46+ """
47+ pass
48+
4049 def _process_finished (self ) -> None :
4150 """
4251 处理子进程的槽 \n
4352 :return: None
4453 """
54+
4555 self .output .emit ((self .FINISHED , "Subprocess finished." ))
4656 self .process = None
4757
@@ -53,7 +63,7 @@ def _handle_stdout(self) -> None:
5363
5464 if self .process :
5565 data = self .process .readAllStandardOutput ()
56- stdout = bytes (data ).decode ("utf8" )
66+ stdout = bytes (data ).decode (getdefaultencoding () )
5767 self .output .emit ((self .STDOUT , stdout ))
5868
5969 def _handle_stderr (self ) -> None :
@@ -64,7 +74,7 @@ def _handle_stderr(self) -> None:
6474
6575 if self .process :
6676 data = self .process .readAllStandardError ()
67- stderr = bytes (data ).decode ("utf8" )
77+ stderr = bytes (data ).decode (getdefaultencoding () )
6878 self .output .emit ((self .STDERR , stderr ))
6979
7080 def _handle_state (self , state : QtCore .QProcess .ProcessState ) -> None :
0 commit comments