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

2024-04-27 11:24:35 发布

您现在位置:Python中文网/ 问答频道 /正文

我用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公司名称:

^{pr2}$

在主.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()

                       }



          }



}

Tags: ortheimportidappsignal错误qt
1条回答
网友
1楼 · 发布于 2024-04-27 11:24:35

错误消息非常清楚:ApplicationWindow不是QQuickItem,因此不能使用QQuickView来加载它。在

原因是ApplicationWindow和QQuickView冲突,因为它们都继承了QQuickWindow。要加载应用程序窗口,您需要使用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()

相关问题 更多 >