2017-04-07 7 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やレイアウトを使用する必要があり、あなたは正しくQMainWindowからQQuickWidgetを追加していないということです。また、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

関連する問題