首先创建一个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_()

说明:

  1. app = QGuiApplication([])创建一个应用
  2. QtQml.QQmlApplicationEngine()创建QML程序的应用引擎
  3. engine.load(path)使用引擎加载QML文件
  4. 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!";
    }
}

说明:

  1. visible: true 是必须的,否则application不会显示
  2. 创建了一个Text组件来显示hello world

直接运行,python test.py,就可以看到熟悉的hello world了。