pyqt5 qml最小示例在退出时崩溃

时间:2017-08-23 20:05:18

标签: python qt qml pyqt5

我有这个小例子应用程序导致崩溃。

main.py

import sys
from PyQt5.QtCore import QUrl
from PyQt5.QtGui import QGuiApplication
from PyQt5.QtQml import QQmlApplicationEngine


def run():
    app = QGuiApplication(sys.argv)

    engine = QQmlApplicationEngine()
    engine.load(QUrl('main.qml'))

    if not engine.rootObjects():
        return -1

    return app.exec_()


if __name__ == '__main__':
sys.exit(run())

main.qml

import QtQuick 2.7
import QtQuick.Controls 2.0
import QtQuick.Layouts 1.3
import QtQuick.Dialogs 1.2

ApplicationWindow {
    visible: true
    width: 640
    height: 480
    title: qsTr("Hello World")

    FileDialog {
        id: fileDialog
        selectFolder: true

        onAccepted: {
            console.log('activated')
        }
    }

    Button {
        anchors.centerIn: parent
        text: "Open"
        onClicked: {
            fileDialog.open()
        }
    }
}

要导致崩溃,只需打开应用程序,单击按钮,选择目录,接受然后关闭应用程序。

控制台显示警告信息:
QObject::startTimer: Timers can only be used with threads started with QThread
然后应用程序崩溃了。任何线索为什么或如何获得错误消息/堆栈跟踪?

环境:Windows 10,python 3.6.1,PyQt5 5.9

2 个答案:

答案 0 :(得分:0)

在调用app.exec()之前,您可以添加以下行:

app.aboutToQuit.connect(lambda: app.closeAllWindows())

Chris link帮助解决了这个问题。退出时不再有应用程序崩溃。

答案 1 :(得分:0)

我发现修复很简单,只需将parent参数传递给QQmlApplicationEngine()

engine = QQmlApplicationEngine(parent=app)