QMutexLocker()使UI释放

2024-10-01 07:30:38 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个控制机器人的控制器类(通过串行接口连接)。此控制器附着到视图。除此之外,我还有一个从QThread派生的线程,它定期读取robot的状态。在

读取状态不能与从用户界面触发的机器人命令一起。因此,我使用QMutexLocker锁定了每个robot访问互斥体,但是如果执行了这样一个互斥体块,这会导致我的userinterface冻结。在

class RobotControl(QObject):
    def __init__(self, view):
        super(RobotControl, self).__init__()
        self.view = view
        self.updatethread = UpdatePositionAndStatus(self.robot)
        self.mutex = QMutex()
        self.connect(self.updatethread, SIGNAL("updateStatus( QString ) "), self.print_error)
        self.updatethread.start()

@pyqtSlot()  
def init_robot(self):
    """
    Initializes the serial interface to the robot interface and checks if
    there is really a robot interface available.
    """
    with QMutexLocker(self.mutex):
        # Open interface
        try:
            index = self.view.robotcontrolui.get_selected_interface_index()
            interface = self.interfaces.item(index).text()
            self.robot = RobotController(interface)
        except DeviceError:
            self.view.error_dlg(self.tr("Couldn't open interface {0}!".format(interface)))
            self.robot = None
            return

        # Check if there is really a robot interface on the selected serial
        # interface with trying to read status byte
        try:
            self.robot.status()
        except DeviceError:
            # In case of failure release interface
            self.close_robot()
            self.view.error_dlg(self.tr("Couldn't initialize robot interface!"))
            return

        self.view.robotcontrolui.bt_open_interface.setEnabled(False)
        self.view.robotcontrolui.bt_close_interface.setEnabled(True)

class UpdatePositionAndStatus(QThread):
    def __init__(self, robot, parent=None):
        QThread.__init__(self, parent) 
        self.robot = robot
        self.mutex = QMutex()
    def run(self):
        """ 
        This function continously reads out the position and the status to for 
        updating it on the userinterface.
        """
        try:
            while True:
                if self.robot is not None:
                    # Do robot communication under a lock
                    self.mutex.lock()
                    (_, rel_pos) = self.robot.read_position()
                    status = self.robot.status()
                    self.mutex.unlock()

                    # Display position and status on userinterface
                    self.view.robotcontrolui.update_position_and_status(rel_pos, status)

                # Wait 1 seccond for next update
                QThread.sleep(1.0)
        except DeviceError:
            # Release lock on robot
            self.mutex.unlock()
            self.emit(SIGNAL("updateStatus( QString )"), self.tr("Error while updating current position and status!"))

在触发init方法之后,用户界面冻结,程序崩溃:为什么会这样?我怎么才能避免呢?在


Tags: andtheselfviewinitondefstatus
1条回答
网友
1楼 · 发布于 2024-10-01 07:30:38

很难说清楚,因为您的代码示例不完整,但我发现此代码存在两个基本问题:

  1. 您正在锁定两个不同的QMutex对象。为了使互斥正常工作,两个线程必须锁定相同的互斥对象。

  2. 您似乎正从更新线程的以下行直接与GUI交互:

    self.view.robotcontrolui.update_position_and_status(rel_pos, status)
    

    在Qt中,只能通过GUI线程执行GUI操作。很有可能是这导致了你的车祸。请参见:http://qt-project.org/doc/qt-4.8/threads.html

相关问题 更多 >