PyQt:定时器无法从另一个线程启动

时间:2011-05-30 19:55:51

标签: python multithreading qt pyqt qthread

我正在使用python制作Qt GUI并且我收到错误:QObject :: startTimer:无法从另一个线程启动计时器。当我运行readModemSnap方法时会发生这种情况。我已经研究了近一个星期,尝试了许多不同的Qt线程设计模式,我在网上发现但不是很有效。

class ModemScopeWindow(QMainWindow, Ui_ModemScope):
def __init__(self, parent=None):
    super(ModemScopeWindow, self).__init__(parent)

    # Set up the user interface from Designer.
    self.setupUi(self)

    self.thread = MainThread()

    """
    signal connections
    """

    self.thread.newSnap.connect(self.updateScene)       
    self.thread.updateStatus.connect(self.setStatus) 


    self.thread.connectionLock.lock()
    self.thread.runLock.lock()

    self.connect(self.runButton, SIGNAL("clicked()"), self.thread.runLock.unlock, Qt.QueuedConnection)

    self.connect(self.connectButton, SIGNAL("clicked()"), self.thread.connectionLock.unlock, Qt.QueuedConnection)


class MainThread(QThread):

newSnap = pyqtSignal(QGraphicsScene)
updateStatus = pyqtSignal(str)
initConnect = pyqtSignal()

def __init__(self, parent = None):
    super(MainThread, self).__init__(parent)

    self.samples = []

    self.connectionLock = QMutex()
    self.runLock = QMutex()        
    self.cliMute = QMutex()

    self._displayCrosshairs = True
    self._displayGrid = True
    self.persistantMode = False
    self.sampleDepth = 1

    self._currentHaam = "4"

    color = QColor(10,255,71)
    self.plotPen = QPen(color)


    self._leftXscene = -VIEW_SIZE/2
    self._topYscene = -VIEW_SIZE/2
    self._rightXscene = VIEW_SIZE/2
    self._bottomYscene = VIEW_SIZE/2
    self._leftXworld = -10.0
    self._topYworld = 10.0
    self._rightXworld = 10.0
    self._bottomYworld = -10.0
    self._scene = QGraphicsScene(self._leftXscene, self._topYscene, VIEW_SIZE, VIEW_SIZE, self)

    self.start(QThread.HighestPriority)

def run(self):

    self.updateStatus.emit("Enter target IP address and press Connect")

    self.connectionLock.lock()
    self.connectModem()

    while(1):
        self.runLock.lock() 
        #compile scene

        self.readModemSnap()
        self.newSnap.emit(self._scene)
        self.runLock.unlock()

def readModemSnap(self):
    self.updateStatus.emit("Reading Modem Snap...")

    print len(self.samples)
    if len(self.samples) >= self.sampleDepth:# and not self.persistantMode:
        self.samples.pop(0)

    self.cliMute.lock()
    temp = cli.getModemSnap()
    self.cliMute.unlock()
    self.samples.append(temp)


    self.cliMute.lock()
    modType = cli.modemRead(80)
    self.cliMute.unlock()

    if((modType | 0x0FFFFFFF) == 0x0FFFFFFF):
        modType = "0";

    else:
        modType = "%x"%modType
        modType = str(modType)


    modType = "0"
    self.updateStatus.emit("Done") 

    self.refresh()

    self._currentHaam = modType[0]
    if self._displayGrid:
        self.plotModulation(self._currentHaam)

    self.handleSnapshotResponse()

    self.updateStatus.emit("Ready to Run")
def refresh(self):

    #delete scene
    items = self._scene.items()

    for x in items:
        self._scene.removeItem(x)

    #repaint the crosshairs
    if self._displayCrosshairs:
        self.plotLine(-VIEW_SIZE,0,+VIEW_SIZE,0, self.plotPen)
        self.plotLine(0, -VIEW_SIZE,0, +VIEW_SIZE, self.plotPen)
        self.plotScaleTicks()

    #repaint grid
    if self._displayGrid:
        self.plotModulation(self._currentHaam)

    self.newSnap.emit(self._scene)

def handleSnapshotResponse(self):

    for x in range(len(self.samples)):
        for sample in self.samples[x]:
            upper = (sample >> 16) & 0xffff;
            lower = sample & 0xffff
            if (upper & 0x8000):
                upper -= 0x10000
            if (lower & 0x8000):
                lower -= 0x10000
            upper = float(upper)/128.0
            lower = float(lower)/128.0
            self.plot(upper, lower)

你可以看到我没有从另一个线程启动任何线程。我使用main来启动UI,创建一个在构造时自动启动的MainThread。当我注释掉本地化问题时,我发现它是在我调用readModemSnap方法中的self.refresh()和self.handleSnapshotResponse()时发现的。任何人都可以指出我做错的方向吗?或任何有关QThreading的教程?提前谢谢

1 个答案:

答案 0 :(得分:1)

这是规则:除了运行Qt事件循环的主线程之外,您不能从任何线程调用任何GUI函数。当您看到有关QTimer的错误时,可能是因为GUI中的某些内容在内部使用了一个计时器,并且它是从另一个线程触发的。

您的案例中最可能的罪魁祸首是您正在从工作线程操作QGraphicsScene。我会尝试重新排列,以便调用MainThread.reload中的代码以响应newSnap信号,而不是之前。