1

我正在尝试使用 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();
}
4

1 回答 1

1

非常基本的例子:

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
    }
}
于 2014-01-08T12:28:55.803 回答