PyQt - Modify GUI from another thread(PyQt - 从另一个线程修改 GUI)
问题描述
我正在尝试从另一个线程修改我的主要布局.但是函数 run() 永远不会被调用我遇到了错误:
<块引用>QObject::setParent: 不能设置父级,新的父级在不同的线程
这是我的代码:
class FeedRetrievingThread(QtCore.QThread):def __init__(self, parent=None):super(FeedRetrievingThread, self).__init__(parent)self.mainLayout = parent.mainLayout定义运行(自我):# 用 self.mainLayout 做事类 MainWindow(QtGui.QDialog):def __init__(self, parent=None):super(MainWindow, self).__init__(parent)self.mainLayout = QtGui.QGridLayout()self.setLayout(self.mainLayout)self.feedRetrievingThread = FeedRetrievingThread(self)self.timer = QtCore.QTimer()self.timer.timeout.connect(self.updateFeed)self.timer.start(1000)def updateFeed(self):如果不是 self.feedRetrievingThread.isRunning():打印正在运行的线程".self.feedRetrievingThread.start()如果 __name__ == '__main__':app = QtGui.QApplication(sys.argv)主窗口 = 主窗口()mainWindow.show()sys.exit(app.exec_())真不明白,为什么用PyQt访问GUI这么难?在 C# 中,您有 Invoke.PyQt 中有这样的东西吗?
我尝试直接从 MainWindow.__init__ 创建线程(不使用计时器),但它也不起作用.
在 Qt 中,您不应该尝试直接从 GUI 线程外部更新 GUI.
相反,让您的线程发出信号并将它们连接到从 GUI 线程内进行必要更新的插槽.
请参阅有关 线程和 QObjects 的 Qt 文档.>
I'm trying to modify my main layout from another thread. But the function run() is never called and i'm having the error:
QObject::setParent: Cannot set parent, new parent is in a different thread
Here's my code:
class FeedRetrievingThread(QtCore.QThread):
def __init__(self, parent=None):
super(FeedRetrievingThread, self).__init__(parent)
self.mainLayout = parent.mainLayout
def run(self):
# Do things with self.mainLayout
class MainWindow(QtGui.QDialog):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.mainLayout = QtGui.QGridLayout()
self.setLayout(self.mainLayout)
self.feedRetrievingThread = FeedRetrievingThread(self)
self.timer = QtCore.QTimer()
self.timer.timeout.connect(self.updateFeed)
self.timer.start(1000)
def updateFeed(self):
if not self.feedRetrievingThread.isRunning():
print 'Running thread.'
self.feedRetrievingThread.start()
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
mainWindow = MainWindow()
mainWindow.show()
sys.exit(app.exec_())
I really don't get it, why is it so difficult to access the GUI with PyQt? In C# you have Invoke. Is there anything of the kind in PyQt?
I tried creating the thread directly from MainWindow.__init__ (without using the timer) but it didn't work either.
In Qt you should never attempt to directly update the GUI from outside of the GUI thread.
Instead, have your threads emit signals and connect them to slots which do the necessary updating from within the GUI thread.
See the Qt documentation regarding Threads and QObjects.
这篇关于PyQt - 从另一个线程修改 GUI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:PyQt - 从另一个线程修改 GUI
基础教程推荐
- 使用生成器和迭代器时 Python 多循环失败 2022-01-01
- 在 Celery 工作人员中捕获 Heroku SIGTERM 以优雅地关 2022-01-01
- Discord.py 缺少必需的参数 2022-01-01
- 用 Python 编写 Fortran 无格式文件 2022-01-01
- 由Python将MP3转换为MIDI(类型错误:无法加载插件:mtg-Melodia:Melodia) 2022-01-01
- 尝试制作WhatsApp机器人 2022-01-01
- 将 x 轴刻度更改为自定义字符串 2022-01-01
- 与常规 dict 相比,Python manager.dict() 非常慢 2022-01-01
- pyserial - 可以从线程 a 写入串行端口,是否阻塞从线程 b 读取? 2022-01-01
- numpy float:比算术运算中内置的慢 10 倍? 2022-01-01
