[英]PyQt QTimer problem QTimer can only be used with threads started with QThread
[英]Why I get “QTimer can only be used with threads started with QThread” messages if I have no QTimer in my code?
当(且仅当)我退出我的应用程序时,这些(且仅这些)重复的消息会出现在命令提示符上:
QObject::startTimer: QTimer can only be used with threads started with QThread
QObject::startTimer: QTimer can only be used with threads started with QThread
QObject::startTimer: QTimer can only be used with threads started with QThread
这对我来说很奇怪,因为我从不在我的代码(或 QThread)中使用 QTimer。 事实上,使用该应用程序时不会发生任何错误或崩溃,因此这实际上并不是真正的问题。 这发生在 Windows 和 Linux 操作系统中。
我所有的进口:
from __future__ import print_function
from PyQt4.QtGui import (QApplication, QMainWindow,
QFileSystemModel, QTreeView, QTableView,
QAbstractItemView, QMenu, QAction, QKeyEvent)
from PyQt4.QtCore import QDir, Qt, SIGNAL, QString, QFileInfo, QCoreApplication
import sys
主要功能:
def main():
app = QApplication(sys.argv)
app.setApplicationName("QFM")
app.setStyle("plastique")
gui = MainWindow()
gui.show()
app.exec_()
也许它可能与 QFileSystemWatcher(由 QFileSystemModel 使用)有关,我猜……也许它使用了一些 QTimer 功能。
我过去也遇到过类似的问题。
QFileSystemModel
文档页面说明如下:
QFileSystemModel.__init__ (self, QObject parent = None)
parent 参数(如果不是 None)会导致 self 由 Qt 而不是 PyQt 拥有。
使用给定的父级构造文件系统模型。
如果您不传递parent
参数,那么 Python 垃圾收集器可能会在错误的时间删除该对象,并且作为副作用会引发您提到的错误。 我的建议是确保你的对象有一个合适的父对象。 我认为它应该解决问题。
PS:我没有检查您使用的每个课程的文档。 也许QFileSystemModel
不是发生这种事情的唯一类。
根据我的经验,当我继承 Qt 类并且该子类的成员之一不是 Qt 层次结构的一部分时,就会发生这种情况。 例如:
class MainWindow(QMainWindow):
def __init__(self, *args, **kwargs):
super(MainWindow, self).__init__(*args, **kwargs)
...
self.my_widget = MyWidget()
...
如果我以这种方式实现MyWidget
,它会在对象销毁时给我QTimer
错误:
class MyWidget(object):
def __init__(self):
# do stuff
但是,如果MyWidget
从QObject
继承,则不会发生错误:
class MyWidget(QObject):
def __init__(self, parent):
super(MyWidget, self).__init__(parent)
#do stuff
如果您没有像 QFileSystemModel(self) 那样子类化它,则将 self 传递给它的实例化
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.