how to use Qt Quick Controls in PySdie2

in general, QQuickView module is used to load QML file like below.

from PySide2.QtWidgets import QApplication
from PySide2.QtQuick import QQuickView
from PySide2.QtCore import QUrl

app = QApplication([])
view = QQuickView()
url = QUrl("view.qml")

view.setSource(url)
view.show()
app.exec_()

but, if we try to load the QML file that has the root of ApplicationWindow, it will be failed.

if you want to load QML file of witch root object is ApplicationWindow, the QML file has to be loaded by QQmlApplicationEngine module.

the example is like below.

//QML
import QtQuick 2.13
import QtQuick.Controls 2.13

ApplicationWindow {
    title: "Window Application"
    width: 640
    height: 480
    visible: true

    Button {
        text: "simple button"
    }
}

//Python

from PySide2.QtCore import QUrl, Qt
from PySide2.QtGui import QGuiApplication
from PySide2.QtQml import QQmlApplicationEngine

QGuiApplication.setAttribute(Qt.AA_EnableHighDpiScaling)
app = QGuiApplication([])
engine = QQmlApplicationEngine()

url = QUrl('view.qml')
engine.load(url)
app.exec_()

if you execute above code, you can see that ApplicationWindow is loaded successfully.


Comments

Popular posts from this blog

making menubar and menu on QML

let's start QML programming with PySide2

QSizePolicy Fixed example in pyside2