Files
dtm-py-all/UI/reserved/test_qml.py

48 lines
899 B
Python
Raw Normal View History

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
简单的QML测试
"""
import sys
from PyQt5.QtWidgets import QApplication
from PyQt5.QtQml import QQmlApplicationEngine
from PyQt5.QtCore import QUrl
# 简单的QML内容
simple_qml = """
import QtQuick 2.15
import QtQuick.Controls 2.15
ApplicationWindow {
visible: true
width: 640
height: 480
title: "测试窗口"
Text {
text: "Hello, PyQt5 QML!"
anchors.centerIn: parent
}
}
"""
def main():
app = QApplication(sys.argv)
# 创建临时QML文件
with open("test.qml", "w", encoding="utf-8") as f:
f.write(simple_qml)
engine = QQmlApplicationEngine()
engine.load(QUrl.fromLocalFile("test.qml"))
if not engine.rootObjects():
print("Failed to load QML")
sys.exit(-1)
sys.exit(app.exec_())
if __name__ == '__main__':
main()