繁体   English   中英

关闭应用程序时多次出现错误“QObject::startTimer: QTimer can only be used with threads started with QThread”

[英]Error “QObject::startTimer: QTimer can only be used with threads started with QThread” many times when closing application

我知道这已经被问过很多次了。 我阅读了所有这些主题,我的情况似乎有所不同。 其他遇到此问题的人都有一些我认为已排除的直接原因,例如:

  • 在没有事件循环运行的情况下启动计时器
  • 从创建计时器的线程以外的线程启动/停止计时器
  • 未设置widget的parent属性,导致销毁顺序问题

下面我有一个演示问题的最小代码示例。 请注意,我没有启动任何线程或计时器。 我还设置了每个小部件的父级。 如果我删除图形小部件,问题就会消失,所以人们很想责怪 pyQtGraph,但是,如果我包含绘图小部件但排除所有空白选项卡(即除 tabCatchaTiger 之外的每个选项卡),问题也会消失,并且似乎证明 pyQtGraph 是正确的。

版本:

  • Windows 7的
  • 蟒蛇 2.7.8
  • 翼IDE 5.0.9-1
  • PyQt 4.11.1
  • PyQwt 5.2.1
  • PyQtGraph 0.9.8

测试用例:

from PyQt4 import Qt, QtGui, QtCore
import PyQt4.Qwt5 as Qwt
import pyqtgraph as pg

pg.functions.USE_WEAVE = False # Lets pyqtgraph plot without gcc

pg.setConfigOption('background', 'w')
pg.setConfigOption('foreground', 'k')

# GUI for visualizing data from database
class crashyGUI(QtGui.QWidget) :

    def __init__(self) :
        # Make the window
        QtGui.QWidget.__init__(self)
        self.resize(700, QtGui.QDesktopWidget().screenGeometry(self).height()*.85)
        self.setWindowTitle('Data Visualization')

        # Create tab interface
        tabWidget = QtGui.QTabWidget(self)

        # define the tab objects
        self.tabEeny = QtGui.QWidget(tabWidget)
        self.tabMeeny = QtGui.QWidget(tabWidget)
        self.tabMiney = QtGui.QWidget(tabWidget)
        self.tabMoe = QtGui.QWidget(tabWidget)
        self.tabCatchaTiger = QtGui.QWidget(tabWidget)
        self.tabByThe = QtGui.QWidget(tabWidget)
        self.tabToe = QtGui.QWidget(tabWidget)

        # Initialize the tab objects
        self.initTabCatchaTiger()

        ###########################################
        ############### Main Layout ###############
        ###########################################

        tabWidget.addTab(self.tabEeny, 'Eeny')
        tabWidget.addTab(self.tabMeeny, 'Meeny')
        tabWidget.addTab(self.tabMiney, 'Miney')
        tabWidget.addTab(self.tabMoe, 'Moe')
        tabWidget.addTab(self.tabCatchaTiger, 'Catch a Tiger')
        tabWidget.addTab(self.tabByThe, 'By The')
        tabWidget.addTab(self.tabToe, 'Toe')

        self.mainLayout = QtGui.QVBoxLayout(self)
        self.mainLayout.addWidget(tabWidget)

        self.setLayout(self.mainLayout)

    def initTabCatchaTiger(self):
        ###########################################
        ############# ADC Capture Tab #############
        ###########################################
        # define tab layout
        grid = QtGui.QGridLayout(self.tabCatchaTiger)

        # create copy of adc plot and add to row 3 of the grid
        self.catchaTigerPlot1 = pg.PlotWidget(name = 'Catch a Tiger 1', parent = self.tabCatchaTiger)
        self.catchaTigerPlot1.setTitle('Catch a Tiger 1')
        grid.addWidget(self.catchaTigerPlot1, 2, 0, 1, 8)

        self.catchaTigerPlot2 = pg.PlotWidget(name = 'Catch a Tiger 2', parent = self.tabCatchaTiger)
        self.catchaTigerPlot2.setTitle('Catch a Tiger 2')
        grid.addWidget(self.catchaTigerPlot2, 3, 0, 1, 8)

        # set layout for tab
        self.tabCatchaTiger.setLayout(grid)

    def closeEvent(self, event) :
            pass

def main() :
    # open a QApplication and dialog() GUI
    app = QtGui.QApplication([])

    windowCrashy = crashyGUI()
    windowCrashy.show()
    app.exec_()

main()

示例中似乎有两个密切相关的问题。

第一个导致 Qt 打印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

第二个(可能不会影响所有用户)导致 Qt 打印QPixmap: Must construct a QApplication before a QPaintDevice ,然后在退出时转储核心。

这两个问题都是由python在退出时以不可预测的顺序删除对象引起的。

在示例中,可以通过将以下行添加到顶级窗口的__init__来修复第二个问题:

    self.setAttribute(QtCore.Qt.WA_DeleteOnClose)

除非QApplication.setQuitOnLastWindowClosed已更改为False ,否则这将确保应用程序在正确的时间退出,并且 Qt 有机会在 python 垃圾收集器开始工作之前自动删除顶级窗口的所有子窗口。

但是,要完全成功,所有相关对象必须在父子层次结构中链接在一起。 示例代码尽可能地执行此操作,但在PlotWidget类的初始化中似乎有一些关键的地方没有完成。

特别是,没有什么可以确保PlotWidget的中心项在PlotWidget具有父集。 如果代码的相关部分改成这样:

class PlotWidget(GraphicsView):
    ...
    def __init__(self, parent=None, background='default', **kargs):
        GraphicsView.__init__(self, parent, background=background)
        ...
        self.plotItem = PlotItem(**kargs)
        # make sure the item gets a parent
        self.plotItem.setParent(self)
        self.setCentralItem(self.plotItem)

那么QTimer消息的第一个问题也消失了。

这是一个更好的答案:

您允许在 python 退出之前收集QApplication 这会导致两个不同的问题:

  1. QTimer 错误消息是由 pyqtgraph 在 QApplication 被销毁后尝试跟踪其 ViewBoxes 引起的。

  2. 崩溃似乎是 Qt / PyQt 固有的。 以下以同样的方式崩溃:

     from PyQt4 import Qt, QtGui, QtCore def main() : app = QtGui.QApplication([]) x = QtGui.QGraphicsView() s = QtGui.QGraphicsScene() x.setScene(s) x.show() app.exec_() main()

您可以通过将global app添加到主函数或通过在模块级别创建QApplication来修复它。

尝试在 __init__ 块中写入:

self.setAttribute(Qt.WA_DeleteOnClose)

就个人而言,我不再努力追赶退出崩溃——只需使用pg.exit()并完成它。

(但如果你碰巧在 pyqtgraph 中发现了一个 bug,请不要犹豫,在 github 上打开一个问题)

我也发生了这种情况,在我的情况下,它是由应用程序的aboutToQuit -Signal 上调用deleteLater()引起的,如下所示:

def closeEvent(self, *args, **kwargs):
   self.deleteLater()

if __name__ == "__main__":

   application = QtWidgets.QApplication(sys.argv)

   window = testApplication()
   # Handle application exit
   application.aboutToQuit.connect(window.closeEvent)
   # System exit
   sys.exit(application.exec_())

摆脱整个窗口上的 deleteLater 似乎解决了它。

暂无
暂无

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

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