为什么我的代码中没有QTimer,却收到“QTimer can only be used with threads started with QThread”的消息?

12

仅当我退出应用程序时,这些(且仅这些)重复的消息才会出现在命令提示符上:

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

这对我来说相当奇怪,因为我从未在我的代码中使用过QTimer(或QThread)。 实际上,使用应用程序时没有发生任何错误或崩溃,所以这实际上不是一个真正的问题。 这种情况在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的特性。


你能指出在你的代码的哪一行引起了这些消息吗? - Kai
绝对不行,这2或3个重复的行是唯一给我的信息!!! :-|。 - iacopo
尝试添加一些控制台输出以查找导致代码行的原因。 - Kai
@user714965:没有其他控制台输出,不可能找到导致问题的代码行。 JanneKarila:我添加了我的导入。因为应用程序仍然非常小,我将测试以前的版本,找到问题开始出现的时刻。 - iacopo
可能是PyQt4 Results in QThread error的重复问题。 - ekhumoro
显示剩余2条评论
3个回答

11

我以前也遇到过类似的问题。

QFileSystemModel文档页面上写道:

QFileSystemModel.__init__ (self, QObject parent = None)

如果未传递parent参数,则Python垃圾回收器可能会在错误的时间删除对象,并引发您提到的错误。我的建议是确保您的对象具有适当的父项,这应该可以解决问题。

用给定的父对象构造文件系统模型。

如果您不传递parent参数,则Python垃圾回收器可能会在错误的时间删除对象,并导致您提到的错误。我的建议是确保您的对象具有适当的父项。我认为这应该可以解决问题。

附:我没有检查您使用的每个类的文档。也许QFileSystemModel并不是唯一出现此问题的类。


我遇到了与“QCompleter”类相同的问题。 - chip

3

据我的经验,当我子类化一个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

1

如果您不想通过继承来使用它,那么在实例化时将 self 传递给它,就像这样 QFileSystemModel(self)


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接