QML to Python



  • I followed more than multi tutorials teaching this but it does not work at all, I use python 3.7 and I want to know how to convert a qml file to a gui in python. If you have how to convert xml to gui in python tell me too.


  • Lifetime Qt Champion

    Hi and welcome to devnet,

    There's nothing to convert.

    main.py:

    #!/usr/bin/env python
    
    import sys
    
    from os.path import join
    from os.path import dirname
    
    from PyQt5.QtCore import QUrl
    from PyQt5.QtGui import QGuiApplication
    from PyQt5.QtQuick import QQuickView
    
    
    if __name__ == "__main__":
    
        app = QGuiApplication(sys.argv)
    
        url = QUrl(join(dirname(__file__), 'main.qml'))
    
        view = QQuickView()
        view.setSource(url)
        view.show()
    
        sys.exit(app.exec_())
    

    main.qml:

    import QtQuick 2.0
    
    Rectangle {
        width: 360
        height: 360
    
        Text {
            text: qsTr("Hello Qt")
            anchors.centerIn: parent
        }
    
        MouseArea {
            anchors.fill: parent
            onClicked: {
                Qt.quit()
            }
        }
    }
    

Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.