1

Python で Qt GUI を作成していますが、次のエラーが表示されます: QObject::startTimer: timers cannot be started from another thread. readModemSnap メソッドを実行すると発生します。私はこれにほぼ 1 週間取り組んできましたが、Web で見つけた 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)

ご覧のとおり、別のスレッドからスレッドを開始していません。メインを使用して、構築時にそれ自体を開始する MainThread を作成する UI を開始します。問題を特定するために行をコメントアウトしたところ、readModemSnap メソッドで self.refresh() と self.handleSnapshotResponse() を呼び出したときであることがわかりました。誰かが間違っている方向に私を向けることができますか? または QThreading に関するチュートリアルはありますか? 前もって感謝します

4

1 に答える 1

2

これがルールです。Qt イベント ループを実行しているメイン スレッド以外のスレッドから GUI 関数を呼び出すことはできません。QTimer に関するエラーが表示される場合、おそらく GUI の何かが内部でタイマーを使用しており、別のスレッドからトリガーされていることが原因です。

あなたの場合の最も可能性の高い原因は、ワーカースレッドから QGraphicsScene を操作していることです。MainThread.reload のコードが newSnap シグナルの前ではなく、それに応じて呼び出されるように再配置してみます。

于 2012-06-19T16:03:45.627 に答える