【问题标题】:Why I get "QTimer can only be used with threads started with QThread" messages if I have no QTimer in my code?如果我的代码中没有 QTimer,为什么我会收到“QTimer 只能用于以 QThread 开头的线程”消息?
【发布时间】:2012-11-13 18:59:23
【问题描述】:

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

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 功能。

【问题讨论】:

  • 你能指出这些消息是在你的代码的哪一行引起的吗?
  • 绝对不是,这2、3行重复的就是给我的唯一信息!!! :-|.
  • 尝试添加一些控制台输出来查找导致代码行。
  • 也许它来自您使用的库。尝试消除部分代码,直到问题消失。这应该会给你一些线索,问题出在哪里。
  • @user714965:没有 no 其他控制台输出,无法找到导致代码行。 JanneKarila:我添加了我的导入。因为应用程序还很小,所以我要去测试以前的版本,以找出问题开始出现的时刻。

标签: python pyqt4 multiplatform qtimer


【解决方案1】:

我过去也遇到过类似的问题。

QFileSystemModeldocumentation page 表示以下内容:

QFileSystemModel.__init__ (self, QObject parent = None)

parent 参数,如果不是 None,则导致 self 归 Qt 所有 而不是 PyQt。

用给定的父级构造一个文件系统模型。

如果您不传递 parent 参数,那么 Python 垃圾收集器可能会在错误的时间删除对象,并作为副作用引发您提到的错误。我的建议是确保你的对象有一个合适的父对象。我认为它应该可以解决问题。

PS:我没有检查你使用的每个课程的文档。也许QFileSystemModel 不是唯一发生这种情况的班级。

【讨论】:

  • 我在QCompleter class 遇到了同样的问题
【解决方案2】:

根据我的经验,当我对 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

【讨论】:

    【解决方案3】:

    如果您没有像这样子类化它,则将 self 传递给它的实例化 QFileSystemModel(self)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-01-11
      • 2012-06-03
      • 2015-08-13
      • 2019-02-01
      • 1970-01-01
      • 1970-01-01
      • 2018-06-27
      • 2011-08-23
      相关资源
      最近更新 更多