【PyQT5+QML】18行代码实现Hello World入门
首先创建一个python文件,test.py
:
from PyQt5.QtGui import QGuiApplication
from PyQt5 import QtQml
if __name__ == '__main__':
path = 'qml/test.qml'
app = QGuiApplication([])
engine = QtQml.QQmlApplicationEngine()
engine.load(path)
app.exec_()
说明:
app = QGuiApplication([])
创建一个应用QtQml.QQmlApplicationEngine()
创建QML程序的应用引擎engine.load(path)
使用引擎加载QML文件app.exec_()
等待退出
然后创建qml文件:
import QtQuick 2.3
import QtQuick.Controls 1.2
ApplicationWindow {
id: test
visible: true
width: 100; height: 100
Text {
text: "hello world!";
}
}
说明:
visible: true
是必须的,否则application不会显示- 创建了一个
Text
组件来显示hello world
直接运行,python test.py
,就可以看到熟悉的hello world
了。