Why I get “QTimer can only be used with threads started with QThread” messages if I have no QTimer in my code?

前端 未结 3 1799
没有蜡笔的小新
没有蜡笔的小新 2021-02-20 17:40

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 wi         


        
3条回答
  •  挽巷
    挽巷 (楼主)
    2021-02-20 18:00

    In my experience this happens when I subclass a Qt class and one of the members of the subclass is not part of the Qt hierarchy. For example:

    class MainWindow(QMainWindow):
        def __init__(self, *args, **kwargs):
            super(MainWindow, self).__init__(*args, **kwargs)
            ...
            self.my_widget = MyWidget()
            ...
    

    If I implement MyWidget in this way, it will give me the QTimer error when the object is destroyed:

    class MyWidget(object):
        def __init__(self):
            # do stuff
    

    However, if MyWidget inherits from QObject then no error occurs:

    class MyWidget(QObject):
        def __init__(self, parent):
            super(MyWidget, self).__init__(parent)
            #do stuff
    

提交回复
热议问题