gpt4 book ai didi

python - PyQt:计时器无法从另一个线程启动

转载 作者:太空宇宙 更新时间:2023-11-03 19:30:25 25 4
gpt4 key购买 nike

我正在使用 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的教程吗?提前致谢

最佳答案

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

您的情况最有可能的罪魁祸首是您正在从工作线程对 QGraphicsScene 进行操作。我会尝试重新安排,以便在响应 newSnap 信号时调用 MainThread.reload 中的代码,而不是在它之前。

关于python - PyQt:计时器无法从另一个线程启动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6180293/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com