Cannot Use ApplicationWindow (QML) On PySide2
I'm trying to run a simple Qt application using QML and more specifically ApplicationWindow. My Python code is a simple QML caller: import sys from PySide2.QtWidgets import QApplic
Solution 1:
First, as pyside returns:
QQuickView does not support using windows as a root item. If you wish to create your root window from QML, consider using QQmlApplicationEngine instead.
This is a working example:
main.py
import sys
from PySide2.QtWidgets import QApplication
from PySide2.QtCore import Qt, QCoreApplication
from PySide2.QtQml import QQmlApplicationEngine
if __name__ == '__main__':
app = QApplication(sys.argv)
QApplication.setAttribute(Qt.AA_EnableHighDpiScaling)
QCoreApplication.setAttribute(Qt.AA_UseHighDpiPixmaps)
engine = QQmlApplicationEngine('view.qml')
sys.exit(app.exec_())
view.qml
import QtQuick.Controls 2.4
import QtQuick 2.0
ApplicationWindow {
title: "Qt Quick Controls Gallery"
visible: true
width: 640
height: 480
Rectangle {
width: 30
height: 30
color: "blue"
}
}
Solution 2:
If you read the docs:
The
QQuickView
class provides a window for displaying a Qt Quick user interface.
That is, QQuickView
provides a window to show Items, but ApplicationWindow
is already a window so you do not need and should not use QQuickView
anymore, in the case of ApplicationWindow
you must use QQmlApplicationEngine
:
main.py
import sys
from PySide2 import QtCore, QtGui, QtQml
if __name__ == '__main__':
QtCore.QCoreApplication.setAttribute(QtCore.Qt.AA_EnableHighDpiScaling)
app = QtGui.QGuiApplication(sys.argv)
engine = QtQml.QQmlApplicationEngine()
url = QtCore.QUrl.fromLocalFile('view.qml')
engine.load(url)
if not engine.rootObjects():
sys.exit(-1)
sys.exit(app.exec_())
view.qml
import QtQuick 2.7
import QtQuick.Controls 2.4
ApplicationWindow {
title: "Qt Quick Controls Gallery"
width: 640
height: 480
visible: true // <---
}
Post a Comment for "Cannot Use ApplicationWindow (QML) On PySide2"