简体   繁体   English

PyQt4导致QThread错误

[英]PyQt4 Results in QThread error

Using PyQt4 4.8.6 the code below produces the error 使用PyQt4 4.8.6,下面的代码会产生错误

QObject::startTimer: QTimer can only be used with threads started with QThread QObject :: startTimer:QTimer只能用于以QThread启动的线程

when a is used as the variable for QApplication, but it does not produce the error if cpp (or most anything else) is used for the variable. 当a用作QApplication的变量时,如果cpp(或大多数其他)用于变量,它不会产生错误。 Is this a bug in PyQt4 or is there something I am missing? 这是PyQt4中的错误还是我缺少的东西?

#! /usr/bin/env python

# This is only needed for Python v2 but is harmless for Python v3.
import sip
sip.setapi('QVariant', 2)

from PyQt4 import QtGui

#def main():

if __name__ == '__main__':
    import sys

    if len(sys.argv) > 1:
       use_a = False
       print "Don't use a"
    else:
       use_a = True
       print "Use a"

    if use_a:
       a = QtGui.QApplication(sys.argv)
    else:
       cpp = QtGui.QApplication(sys.argv)

    model = QtGui.QStandardItemModel(4,2)
    tableView = QtGui.QTableView()
    tableView.setModel(model)

    tableView.show()
    if use_a:
       sys.exit(a.exec_())
    else:
       sys.exit(cpp.exec_())


#if __name__ == '__main__':
#  main()

It is probably not a bug, as such. 这可能不是一个bug。

When the Python begins to shut down, the order in which objects get garbage-collected can be unpredictable. 当Python开始关闭时,对象被垃圾收集的顺序可能是不可预测的。 The error message you are seeing is most likely a side-effect of that. 您看到的错误消息很可能是副作用。

Is this causing a real problem in your application? 这是否会在您的应用程序中造成真正的问题?

If not, just rename as appropriate and forget about it... 如果没有,只需重新命名并忘记它......

You need to set the view to delete when it is closed. 您需要将视图设置为在关闭时删除。 This just entails adding the following two lines to your application: 这只需要在您的应用程序中添加以下两行:

from PyQt4.QtCore import Qt

and then after the tableView is instantiated: 然后在实例化tableView之后:

tableView.setAttribute(Qt.WA_DeleteOnClose)

When I add those lines to your code I don't get the error. 当我将这些行添加到您的代码中时,我没有收到错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM