let's start QML programming with PySide2

What is QML?

QML is UI programming language. it is some like the structure of JSON format. QML is supported by QtQML module. In other words, it is based in Qt framework.
QtQuick module includes QtQML module. and QtQuick includes visual components, model-view support, animation framework and other many components to build UI.

What is strength of QML?

QML is derived from Qt. using QML means using Qt. Qt is cross-platform framework. so, once you are familiar with Qt, you can make desktop, mobile(andorid and ios) and embedded GUI application with only Qt knowledge. and you can choose what you use for back-end with c++ and python.


when learning new programming, it would be best way to print "Hello World".
so, I will show you how to print "Hello World" with QML and Python.

first of all, you should make .qml file. I made View.qml file for example. and then, just type like below code.

//View.qml

import QtQuick 2.0

Rectangle {
    width: 200
    height: 200
    color: "green"

    Text {
        text: "Hello World"
        anchors.centerIn: parent
    }
}


we could ignore what the code is meaning in this time. just copy and paste it on your side.

after that, you need to make python file to load .qml file. I made main.py for example.


//main.py

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.setResizeMode(QQuickView.SizeRootObjectToView)
view.show()

app.exec_()
del view

when we make Qt application, we have to make QApplication first. and then, we should make view part. it is general thing when using Qt framework.
and then, I loaded QML file by QQuickView object like below.


view.setSource(url)

as the result, we can get below screen.







Comments

Popular posts from this blog

making menubar and menu on QML

QSizePolicy Fixed example in pyside2