[SOLVED] About showing QWindow
-
hi guys
i am working on a project that use PyQt5 and QML.
i have this two files and they work:
PyQt file:
@import sys
from PyQt5.QtCore import QUrl
from PyQt5.QtWidgets import QApplication
from PyQt5.QtQuick import QQuickViewapp = QApplication(sys.argv)
view = QQuickView()
view.setSource(QUrl("example.qml"))
view.show()sys.exit(app.exec_())@
and QML File:
@import QtQuick 2.0
Item {
id: root
width: 300; height: 100Rectangle {
id: bg
anchors.fill: parent
color: "red"Text { id: helloworld text: "Hello world!" anchors.centerIn: parent color: "White" font.pointSize: 20 }
}
}@this project work.
but question here:
if instead of Item type that is instanced from QQuickItem, i use ApplicationWindow that instanced from QWindow, QQuickView can not show the window for me.
i want to know that what Class of QtQuick should i use to show ApplicationWindow from PyQt?PyQt File: (same as before)
@import sys
from PyQt5.QtCore import QUrl
from PyQt5.QtWidgets import QApplication
from PyQt5.QtQuick import QQuickViewapp = QApplication(sys.argv)
view = QQuickView()
view.setSource(QUrl("example.qml"))
view.show()sys.exit(app.exec_())@
QML File:
@import QtQuick 2.0
import QtQuick.Controls 1.0ApplicationWindow {
id: root
width: 300; height: 100
title: "my window"Rectangle {
id: bg
anchors.fill: parent
color: "red"Text { id: helloworld text: "Hello world!" anchors.centerIn: parent color: "White" font.pointSize: 20 }
}
}@the error is : QQuickView only supports loading of root objects that derive from QQuickItem.
thanks in advance
-
Hi, if you want to use an QML ApplicationWindow as the root element you can't use QQuickView. I don't know if there is any official example for it but I can show you I use it in my app, this is c++ though, but I think you can easily convert it to Python:
@
QQmlApplicationEngine engine(QUrl("qrc:///main.qml"));
QObject *topLevel = engine.rootObjects().value(0);
QQuickWindow window = qobject_cast<QQuickWindow>(topLevel);
window->show();
@
so you have to create the QQmlApplicationEngine yourself and just pass it your QML file containing the ApplicationWindow.
That is at least how I use it. :) -
Thanks
i try to change it to python
but i don't know how change this line to it:
@QQuickWindow window = qobject_cast<QQuickWindow>(topLevel);@because python don't have pointer..
-
I have no experience with Python, but since it is a scripting language I guess you can just ignore that part and call the "show" function, try this:
@
engine = QQmlApplicationEngine(QUrl("qrc:///main.qml"))
window = engine.rootObjects().value(0)
window.show()
@
( this is my first "phython" script so I have no idea if that is valid code, just used your code as example haha) -
oh , yes.
i don't know that line but as you said i ignore it and this code works in PyQt:
@import sys
from PyQt5.QtCore import QUrl
from PyQt5.QtGui import QGuiApplication
from PyQt5.QtQml import QQmlApplicationEngineapp = QGuiApplication(sys.argv)
engine = QQmlApplicationEngine(QUrl("example.qml"))
window = engine.rootObjects()[0]
window.show()sys.exit(app.exec_())@
really thanks dear Xandar