gpt4 book ai didi

python - QQuickView 仅支持加载从 QQuickItem 派生的根对象错误?

转载 作者:太空狗 更新时间:2023-10-30 00:44:42 25 4
gpt4 key购买 nike

我使用 pyqt5 编写了简单的 hello world。但是当我启动它时出现错误:

QQuickView only supports loading of root objects that derive from QQuickItem. 

If your example is using QML 2, (such as qmlscene) and the .qml file you
loaded has 'import QtQuick 1.0' or 'import Qt 4.7', this error will occur.

To load files with 'import QtQuick 1.0' or 'import Qt 4.7', use the
QDeclarativeView class in the Qt Quick 1 module.

我试图解决它,但我想我不明白发生了什么。有人可以更详细地解释这个错误吗?我该如何解决?

主要.py:

#!/usr/bin/python3.4


from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.Qt import *
from PyQt5.QtQuick import *

if __name__=='__main__':
import os
import sys


class Main(QObject):
def __init__(self,parent=None):
super().__init__(parent)
self.view=QQuickView()
self.view.setSource(QUrl.fromLocalFile('main.qml'))
def show(self):
self.view.show()



app=QApplication(sys.argv)
main=Main()
main.show()
sys.exit(app.exec_())

主.qml

import QtQuick 2.2
import QtQuick.Controls 1.1
import QtQuick.Window 2.0



ApplicationWindow
{
signal btnPlayClicked()
signal btnStopClicked()



id:app
width:Screen.desktopAvailableWidth
height:Screen.desktopAvailableHeight
color:"black"


ToolBar{
y:app.height-height
height:btnPlay.height
Button
{
id:btnPlay
x:app.width/2-btnPlay.width
text:"Play"
onClicked: parent.parent.btnPlayClicked()

}
Button
{
id:btnStop
x:app.width/2
text:"Stop"
onClicked: parent.parent.btnStopClicked()

}



}



}

最佳答案

错误信息很清楚:ApplicationWindow 不是 QQuickItem,因此您不能使用 QQuickView 加载它。

之所以,是因为ApplicationWindow和QQuickView是有冲突的,因为他们都继承了QQuickWindow。为了加载 ApplicationWindow,您需要使用 QQmlApplicationEngine :

class Main(QObject):
def __init__(self,parent=None):
super().__init__(parent)
self.engine = QQmlApplicationEngine(self)
self.engine.load(QUrl.fromLocalFile('main.qml'))
self.window = self.engine.rootObjects()[0]

def show(self):
self.window.show()

关于python - QQuickView 仅支持加载从 QQuickItem 派生的根对象错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25931508/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com