2014-09-19 94 views
4

我使用pyqt5.But寫了簡單的Hello World,當我啓動它,我得到錯誤:QQuickView僅支持加載從QQuickItem錯誤派生的根對象?

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. 

我試圖解決這個問題,但我想我不明白是什麼happend.Can有人解釋我在更多這個錯誤細節和我如何解決它?

Main.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_()) 

Main.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() 

         } 



      } 



} 
+0

我一直在尋找了很長的時間完全相同的問題幫助了,但沒有找到,直到現在。 – 2015-11-22 14:11:40

回答

8

的錯誤信息是很清楚的: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() 
+1

好的,非常感謝 – 2014-09-19 18:16:13

+1

我希望我能再次upvote ;-) – 2015-11-22 14:12:29

+0

答案C++:[這裏](https://stackoverflow.com/questions/18506784/qml-and-c-with-qt-quick-2-應用) – 2017-09-11 15:59:24

相關問題