我有我的新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}
}
]
}
你可以放置QML代碼 – eyllanesc
我添加的代碼 – Polly