Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

PySide 2 Sample application doesn't quit!



  • Python Source:

    import sys
    from PySide2.QtGui import QGuiApplication
    from PySide2.QtCore import QCoreApplication, Qt, QUrl
    from PySide2.QtQuick import QQuickView
    
    QCoreApplication.setAttribute(Qt.AA_EnableHighDpiScaling)
    app = QGuiApplication(sys.argv)
    
    view = QQuickView(QUrl('view.qml'))
    view.show()
    
    app.exec_()
    sys.exit(0)
    
    
    
    
    

    QML file:

    import QtQuick 2.3
    import QtQuick.Controls 1.4
    
    Rectangle {
        width: 200
        height: 200
        color: "green"
    
        Button {
            text: "Hello World"
            anchors.centerIn: parent
            onClicked: Qt.quit()
        }
    }
    

  • Lifetime Qt Champion

    Hi,

    What version of Qt ?
    On what OS ?
    How did you install Qt ?



  • Qt 5.5.1 (MSVC 2013, 32bit)
    Windows 10 Pro 64bit
    i got qt from a website that had an offline version build against mingw
    and i installed PySide2 using Python 3 pip module


  • Lifetime Qt Champion

    I forgot: what version of PySide2 did you get with pip ?



  • PySide2 version is 5.12.3
    shiboken2 version is 5.12.3
    they both are for py3.7-win-amd64



  • Update on the situation i ran the code through command prompt directly and i received this error when i clicked the button:

    Signal QQmlEngine::quit() emitted, but no receivers connected to handle it.
    

    i googled the problem and it seems Qt has a new syntax and for this to work, I have to somehow make a connection between my .py and .qml file.

    sorry for the late reply. I HAVE TO WAIT 600 SECONDS :D



  • The docs notes:

        quit()
    
    This function causes the QQmlEngine::quit() signal to be emitted. Within the Prototyping with qmlscene, this causes the launcher application to exit; to quit a C++ application when this method is called, connect the QQmlEngine::quit() signal to the QCoreApplication::quit() slot.
    
    

    Clearly point out that you have to connect the QQmlEngine::quit() of QQuickView to QCoreApplication::quit():

    import os
    import sys
    
    from PySide2.QtCore import QCoreApplication, Qt, QUrl
    from PySide2.QtGui import QGuiApplication
    from PySide2.QtQuick import QQuickView
    
    if __name__ == '__main__':
        QCoreApplication.setAttribute(Qt.AA_EnableHighDpiScaling)
        app = QGuiApplication(sys.argv)
    
        current_dir = os.path.dirname(os.path.realpath(__file__))
        filename = os.path.join(current_dir, 'view.qml')
        view = QQuickView(QUrl.fromLocalFile(filename))
        view.engine().quit.connect(QCoreApplication.quit)           <---
        view.show()
    
        sys.exit(app.exec_())
    

    Credits to eyllanesc
    https://stackoverflow.com/questions/57581974/qt-error-signal-qqmlenginequit-emitted-but-no-receivers-connected-to-handl


  • Lifetime Qt Champion

    Glad you found out and thanks for sharing !


Log in to reply