为什么我得到"QTimer只能用于以QThread开头的线程"如果我的代码中没有QTimer,则会显示一条消息? [英] Why I get "QTimer can only be used with threads started with QThread" messages if I have no QTimer in my code?

查看:260
本文介绍了为什么我得到"QTimer只能用于以QThread开头的线程"如果我的代码中没有QTimer,则会显示一条消息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

When (and only when) I quit my application, these (and only these) repeated message appear on the command prompt:

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操作系统中都会发生这种情况.

This is quite strange for me, because I never use QTimer in my code (or QThread). In fact, no errors or crashes happen using the application, so this is not a real problem, actually. This happen in both Windows and Linux OSs.

我所有的进口货

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

Perhaps it could be something related to QFileSystemWatcher (used by QFileSystemModel), I guess...maybe it uses some QTimer features.

推荐答案

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

I've had similar problems in the past.

QFileSystemModel 文档页面表示以下内容:

QFileSystemModel.__init__ (self, QObject parent = None)

父参数(如果不是None的话)将导致自己归Qt所有 而不是PyQt.

The parent argument, if not None, causes self to be owned by Qt instead of PyQt.

使用给定的父级构建文件系统模型.

Constructs a file system model with the given parent.

如果不传递parent参数,则Python垃圾收集器可以在错误的时间删除对象,并且副作用是会引起您提到的错误.我的建议是确保您的对象具有适当的父对象.我认为这应该可以解决问题.

If you don't pass a parent argument then the Python garbage collector can delete the object at the wrong time and as a side effect raise the error you mention. My advise is to make sure that your objects have a proper parent. I think it should fix the problem.

PS:我没有检查您使用的每个类的文档.也许QFileSystemModel不是发生这种情况的唯一类.

PS: I haven't checked the docs for every class you use. Maybe QFileSystemModel is not the only class on which this thing happens.

这篇关于为什么我得到"QTimer只能用于以QThread开头的线程"如果我的代码中没有QTimer,则会显示一条消息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆