python - State of Rectangle changes only when Clicking PyQt5 QML -
i have new pyqt5 application. wanted add inside qmainwindow qquickwidget , set properties qml. do:
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")) into qml file create rectangle states should changed when mouse hovering button. when hovered - nothing going on. state changes when click on button , when click , leave button. me, please. how can make right?
full qml code:
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} } ] }
the problem have not added qquickwidget qmainwindow correctly, must use setcentralwidget or layouts place them. qml has error easing.type part of propertyanimation , not of propertychanges.
import sys 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;} } ] }
Comments
Post a Comment