【问题标题】:State of Rectangle changes only when Clicking PyQt5 QML矩形状态仅在单击 PyQt5 QML 时发生变化
【发布时间】:2017-04-07 14:55:23
【问题描述】:

我有我的新 PyQt5 应用程序。我想在 QMainWindow 中添加 QQuickWidget 并使用 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代码
  • 我添加了代码

标签: python pyqt qml pyqt5


【解决方案1】:

问题是你没有正确添加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;}
        }
    ]
}

【讨论】:

  • 谢谢你,@eyllanesc,setCentralWidget 工作正常 :)
猜你喜欢
  • 2018-01-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-10-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多