PyQt4 Results in QThread error
Asked Answered
R

2

6

Using PyQt4 4.8.6 the code below produces the error

QObject::startTimer: QTimer can only be used with threads started with 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. Is this a bug in PyQt4 or is there something I am missing?

#! /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()
Reck answered 24/1, 2012 at 22:44 Comment(0)
A
6

It is probably not a bug, as such.

When the Python begins to shut down, the order in which objects get garbage-collected can be unpredictable. 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...

Afferent answered 24/1, 2012 at 23:44 Comment(5)
It isn't a problem, but I wanted to understand what is going on.Reck
I have the same problem, is it possible to remove such error messages from console?Demicanton
Is it possible to manually delete the thread in order to prevent these error messages??Demicanton
@OrclUser. If the above answer doesn't help, then you've likely got some PyQt objects hanging around that Qt didn't take ownership of. You need to give these objects a parent and/or keep a reference to them. It's impossible to be more precise than this without seeing real code. A crude way to find out which objects are causing the problem is to simply comment out chunks of code until the messages go away, and then gradually add it back in. It won't take long to narrow the problem down.Afferent
Just set the view to delete on close. I've added an answer to show how.Wren
W
0

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.setAttribute(Qt.WA_DeleteOnClose)

When I add those lines to your code I don't get the error.

Wren answered 13/12, 2015 at 18:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.