2017-04-07 57 views
0

我有我的新PyQt5應用程序。我想在QMainWindow中添加QQuickWidget,並用QML設置他的屬性。這是我做的:只有當點擊PyQt5時,矩形狀態纔會改變QML

class mainWindow(QtWidgets.QMainWindow): 
    def __init__(self): 
     super(mainWindow,self).__init__() 
     self.setGeometry(100,100,800,600) 

     engine = PyQt5.QtQml.QQmlEngine(self) 
     view = QtQuickWidgets.QQuickWidget(engine,self) 
     view.setSource(PyQt5.QtCore.QUrl("files/newqml.qml")) 

到QML文件創建與各國矩形應該當鼠標懸停按鈕進行更改。但是當它被徘徊時 - 什麼都沒有發生。當我點擊按鈕並點擊並離開按鈕時,狀態發生變化。請幫幫我。我怎樣才能做到這一點?
全部QML代碼:

import QtQuick 2.3 
import QtQuick.Controls 1.2 
import QtQuick.Window 2.2 
import QtQuick.Controls.Styles 1.2 

Rectangle{ 
    signal buttonPressedSignal 
    signal buttonReleasedSignal 
    id: topButton 
    width:80 
    height: 40 
    color: 'white' 
    border {width: 2; color: '#4CAF50'} 
    state: 'Normal' 
    Text { 
    id: buttonText 
    anchors.centerIn: parent 
    text:'Button' 
    font.pixelSize: 20 
    font.family: 'Hallo sans' 
    color: 'black' 
    } 
    MouseArea{ 
    anchors.fill: topButton 
    hoverEnabled: true 
    onPressed: parent.buttonPressedSignal() 
    onReleased: parent.buttonReleasedSignal() 
    onEntered: parent.state='NotNormal' 
    onExited: parent.state = 'Normal' 
    } 
    states:[ 
    State{ 
     name: 'Normal'; 
     PropertyChanges{target:buttonText;color:'black';easing.type:Easing.InOutElastic} 
    }, 
    State{ 
     name:'NotNormal'; 
     PropertyChanges{target:buttonText;color:'white';easing.type:Easing.InOutElastic} 
    } 
    ] 
    transitions:[ 
    Transition{ 
    to: '*' 
    ColorAnimation{target:buttonText;duration:400} 
    } 
    ] 
} 
+0

你可以放置QML代碼 – eyllanesc

+0

我添加的代碼 – Polly

回答

0

的問題是,你有沒有正確添加QQuickWidgetQMainWindow,你必須使用setCentralWidget或佈局來放置它們。此外qml有一個錯誤easing.type是PropertyAnimation的一部分,而不是PropertyChanges

import sys 
from PyQt5 import QtWidgets, QtQml, QtQuickWidgets, QtCore 


class mainWindow(QtWidgets.QMainWindow): 
    def __init__(self): 
     super(mainWindow,self).__init__() 
     self.setGeometry(100,100,800,600) 

     engine = QtQml.QQmlEngine(self) 
     view = QtQuickWidgets.QQuickWidget(engine,self) 
     view.setSource(QtCore.QUrl("files/newqml.qml")) 
     self.setCentralWidget(view) 


if __name__ == '__main__': 
    app = QtWidgets.QApplication(sys.argv) 
    w = mainWindow() 
    w.show() 
    sys.exit(app.exec_()) 

.qml

import QtQuick 2.3 

Rectangle{ 
    signal buttonPressedSignal 
    signal buttonReleasedSignal 
    id: topButton 
    width:80 
    height: 40 
    color: 'white' 
    border {width: 2; color: '#4CAF50'} 
    state: 'Normal' 
    Text { 
     id: buttonText 
     anchors.centerIn: parent 
     text:'Button' 
     font.pixelSize: 20 
     font.family: 'Hallo sans' 
     color: 'black' 
    } 
    MouseArea{ 
     anchors.fill: topButton 
     hoverEnabled: true 
     onPressed: parent.buttonPressedSignal() 
     onReleased: parent.buttonReleasedSignal() 
     onEntered: parent.state='NotNormal' 
     onExited: parent.state = 'Normal' 
    } 
    states:[ 
     State{ 
      name: 'Normal'; 
      PropertyChanges{target:buttonText;color:'black';} 
     }, 
     State{ 
      name:'NotNormal'; 
      PropertyChanges{target:buttonText;color:'white';} 
     } 
    ] 
    transitions:[ 
     Transition{ 
      to: '*' 
      ColorAnimation{target:buttonText;duration:400} 
      PropertyAnimation{target:buttonText; easing.type:Easing.InOutElastic;} 
     } 
    ] 
} 
+0

謝謝@eyllanesc,setCentralWidget工作正常:) – Polly

相關問題