54 lines
1.2 KiB
Python
54 lines
1.2 KiB
Python
# -*- coding: utf-8 -*-
|
|
"""简单测试 QML 加载"""
|
|
import sys
|
|
from PySide6.QtGui import QGuiApplication
|
|
from PySide6.QtQml import QQmlApplicationEngine
|
|
from PySide6.QtCore import QUrl
|
|
import os
|
|
|
|
app = QGuiApplication(sys.argv)
|
|
|
|
# 创建一个非常简单的 QML
|
|
simple_qml = """
|
|
import QtQuick 2.12
|
|
import QtQuick.Controls 2.12
|
|
|
|
ApplicationWindow {
|
|
visible: true
|
|
width: 400
|
|
height: 300
|
|
title: "Test"
|
|
|
|
Text {
|
|
anchors.centerIn: parent
|
|
text: "QML Loaded Successfully!"
|
|
font.pixelSize: 20
|
|
}
|
|
}
|
|
"""
|
|
|
|
# 先尝试从字符串加载
|
|
engine = QQmlApplicationEngine()
|
|
engine.loadData(simple_qml.encode('utf-8'))
|
|
|
|
if engine.rootObjects():
|
|
print("OK: QML loaded successfully from string")
|
|
sys.exit(app.exec())
|
|
else:
|
|
print("FAIL: Failed to load QML from string")
|
|
# 尝试从文件加载
|
|
test_file = "test_simple.qml"
|
|
with open(test_file, 'w', encoding='utf-8') as f:
|
|
f.write(simple_qml)
|
|
|
|
engine2 = QQmlApplicationEngine()
|
|
engine2.load(QUrl.fromLocalFile(os.path.abspath(test_file)))
|
|
|
|
if engine2.rootObjects():
|
|
print("OK: QML loaded successfully from file")
|
|
sys.exit(app.exec())
|
|
else:
|
|
print("FAIL: Failed to load QML from file")
|
|
sys.exit(-1)
|
|
|