我正在尝试使用PyQt5加载ApplicationWindow实例。我正在阅读它是如何完成它的C ++,这是正确的答案。我如何使用PyQt5创建它? Python中的文档与C ++的文档略有不同。
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QQuickWindow>
int main(int argc, char *argv[])
{
QGuiApplication app(argc, argv);
QQmlApplicationEngine engine;
engine.load(QUrl("qml/window/main.qml"));
QObject *topLevel = engine.rootObjects().value(0);
QQuickWindow *window = qobject_cast<QQuickWindow *>(topLevel);
window->show();
return app.exec();
}
答案 0 :(得分:0)
非常基本的例子:
example.py
:
from PyQt5.QtCore import QUrl
from PyQt5.QtWidgets import QApplication
from PyQt5.QtQuick import QQuickView
import sys
app = QApplication(sys.argv)
view = QQuickView()
view.setSource(QUrl("example.qml"))
view.show()
app.exec()
example.qml
:
import QtQuick 2.0
Rectangle {
width: 300; height: 100
color: "gray"
Text {
id: helloworld
text: "Hello world!"
color: "blue"
font.pointSize: 32
}
}