2017-04-07 6 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

문제 당신이 그들을 배치 setCentralWidget 또는 레이아웃을 사용해야 올바르게 QMainWindowQQuickWidget을 추가하지 않은 것입니다. 또한 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

관련 문제