【问题标题】:How to fix QPropertyAnimation "starting an animation without end value" error如何修复 QPropertyAnimation“开始没有结束值的动画”错误
【发布时间】:2019-11-01 09:45:37
【问题描述】:

我正在尝试将 qt 示例从 c++ 移植到 pyqt/pyside2。此示例显示了使用 QPropertyAnimation 对文本编辑字段进行隐藏/显示操作。

看起来并不复杂,但我还是想不通为什么它不能正常工作

主要思想是:textEdit 小部件通过按下 ">" 按钮“折叠”,并在再次按下按钮时返回。

这是我的python代码:

import PySide2
from PySide2.QtWidgets import *
from PySide2.QtCore import *
from PySide2.QtGui import *
import sys

class myWidget(QWidget):
    def __init__(self):
        super(myWidget, self).__init__()

        self.m_deltaX = 0
        self.m_isClosed = False

        self.btn = QPushButton(self)
        self.btn.setText(">")
        self.btn.setCheckable(True)
        self.btn.setFixedSize(QSize(25, 25))
        self.btn.connect(SIGNAL("clicked()"), self.closeOpenEditor)

        self.text1 = QTextEdit(self)
        self.text1.setText("some sample text")

        self.text2 = QTextEdit(self)

        self.layout_btn = QVBoxLayout()
        self.layout_btn.addWidget(self.btn)

        self.layout_m = QHBoxLayout()
        self.layout_m.addWidget(self.text1, 10)
        self.layout_m.addSpacing(15)
        self.layout_m.addLayout(self.layout_btn)
        self.layout_m.setSpacing(0)
        self.layout_m.addWidget(self.text2, 4)

        self.setLayout(self.layout_m)
        self.resize(800, 500)

    def closeOpenEditor(self):
        self.m_isClosed = self.btn.isChecked()

        animation1 = QPropertyAnimation(self.text2, b"geometry")

        if self.m_isClosed:
            self.text2.setMaximumWidth(self.text2.width())
            text2Start = int(self.text2.maximumWidth())

            self.m_deltaX = text2Start
            text2End = int(3)

            animation1.setDuration(250)
            animation1.setStartValue(text2Start)
            animation1.setEndValue(text2End)

            self.btn.setText("<")
        else:
            text2Start = int(self.text2.maximumWidth())
            text2End = int(self.m_deltaX)

            animation1.setDuration(250)
            animation1.setStartValue(text2Start)
            animation1.setEndValue(text2End)

            self.btn.setText(">")

        animation1.start()

    def resizeEvent(self, event:QResizeEvent):
        if not self.m_isClosed:
            self.text2.setMaximumWidth(QWIDGETSIZE_MAX)



if __name__ == '__main__':
    app = QApplication(sys.argv)
    w = myWidget()
    w.show()
    sys.exit(app.exec_())

我还会添加c++代码:

ma​​inwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QTextEdit>
#include <QPushButton>
#include <QHBoxLayout>

class MyWidget : public QWidget
{

    Q_OBJECT

    QTextEdit       *m_textEditor1;
    QTextEdit       *m_textEditor2;
    QPushButton     *m_pushButton;
    QHBoxLayout     *m_layout;
    QVBoxLayout     *m_buttonLayout;

    int              m_deltaX;
    bool             m_isClosed;


public:

    MyWidget(QWidget * parent = 0);
    ~MyWidget(){}

    void resizeEvent( QResizeEvent * event );

private slots:
    void closeOrOpenTextEdit2(bool isClosing);

};

#endif // MAINWINDOW_H

ma​​in.cpp

#include <mainwindow.h>
#include <QPropertyAnimation>
#include <QApplication>
#include <QIcon>



MyWidget::MyWidget(QWidget * parent):QWidget(parent),m_deltaX(0)
{

  m_pushButton = new QPushButton(this);
  m_pushButton->setText(">");
  m_pushButton->setCheckable(true);
  m_pushButton->setFixedSize(25,25);
  //m_pushButton->setStyleSheet("background-color: yellow;");
  connect(m_pushButton, SIGNAL(clicked(bool)), this, SLOT(closeOrOpenTextEdit2(bool)));

  m_textEditor1 = new QTextEdit(this);
  m_textEditor1->setText("И рвется в пляс душа моя, головой я выбиваю дверцы...");

  m_textEditor2 = new QTextEdit(this);

  m_buttonLayout = new QVBoxLayout();
  m_buttonLayout->addWidget(m_pushButton);
  m_buttonLayout->addItem( new QSpacerItem(1, 1, QSizePolicy::Minimum, QSizePolicy::Expanding) );


  m_layout = new QHBoxLayout;
  m_layout->addWidget(m_textEditor1, 10);
  m_layout->addSpacing(15);
  m_layout->addLayout(m_buttonLayout);
  m_layout->setSpacing(0);
  m_layout->addWidget(m_textEditor2, 4);

  setLayout(m_layout);
  resize(800,500);
}

void MyWidget::closeOrOpenTextEdit2(bool isClosing)
{
    m_isClosed = isClosing;
    QPropertyAnimation *animation1 = new QPropertyAnimation(m_textEditor2, "maximumWidth");

    if(isClosing) //close the second textEdit
    {
        m_textEditor2->setMaximumWidth(m_textEditor2->width());

        int textEdit2_start = m_textEditor2->maximumWidth();

        m_deltaX = textEdit2_start;
        int textEdit2_end = 3;



        animation1->setDuration(250);
        animation1->setStartValue(textEdit2_start);
        animation1->setEndValue(textEdit2_end);


        m_pushButton->setText("<");

    }
    else //open
    {


        int textEdit2_start = m_textEditor2->maximumWidth();
        int textEdit2_end = m_deltaX;


        animation1->setDuration(250);
        animation1->setStartValue(textEdit2_start);
        animation1->setEndValue(textEdit2_end);


        m_pushButton->setText(">");
        //m_pushButton->setIcon()

    }

    animation1->start();

}


void MyWidget::resizeEvent( QResizeEvent * event )
{
    if(!m_isClosed)
        m_textEditor2->setMaximumWidth( QWIDGETSIZE_MAX );
}


int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MyWidget w;
    w.show();

    return a.exec();
}

在我重写的代码中,它不想启动动画。您应该按两次按钮,然后它将永久折叠小部件而没有任何动画。所以你不能再次打开它。

我也收到了一些错误:

QPropertyAnimation::updateState(geometry, QTextEdit, ): 开始一个没有结束值的动画

和

NameError:名称“QWIDGETSIZE_MAX”未定义

【问题讨论】:

    标签: python c++ qt pyside2


    【解决方案1】:

    您的代码有以下错误:

    • 在 C++ 代码中修改的属性是 maximumWidth,但在您的 python 代码中使用几何,在 maximumWidth 的情况下,需要一个整数,但几何需要一个 QRect,因此您会收到错误消息,因为 endValue 是类型不正确,因此通过指出它没有建立值来建立它。

    • 在 Python 中,局部变量在其作用域结束时会被删除,但 PyQt 的情况除外,如果局部变量是一个 QObject,其父 QObject 具有更大作用域的另一个 QObject。在C++的情况下除了使用父类来延长生命周期就是使用指针。

    • QWIDGETSIZE_MAX 未在 PySide2 中定义(但在 PyQt5 中定义:QtWidgets.QWIDGETSIZE_MAX),因此您必须定义一个变量,该变量采用 C++ 中的值。

    from PySide2 import QtCore, QtWidgets
    
    # https://code.qt.io/cgit/qt/qtbase.git/tree/src/widgets/kernel/qwidget.h#n873
    QWIDGETSIZE_MAX = (1 << 24) - 1
    
    class MyWidget(QtWidgets.QWidget):
        def __init__(self):
            super(MyWidget, self).__init__()
            self.m_deltaX = 0
            self.btn = QtWidgets.QPushButton(
                ">", checkable=True, clicked=self.closeOpenEditor
            )
            self.btn.setFixedSize(QtCore.QSize(25, 25))
    
            self.text1 = QtWidgets.QTextEdit()
            self.text1.setText("some sample text")
    
            self.text2 = QtWidgets.QTextEdit()
    
            layout_btn = QtWidgets.QVBoxLayout()
            layout_btn.addWidget(self.btn)
    
            lay = QtWidgets.QHBoxLayout(self)
            lay.addWidget(self.text1, 10)
            lay.addSpacing(15)
            lay.addLayout(layout_btn)
            lay.setSpacing(0)
            lay.addWidget(self.text2, 4)
    
            self.resize(800, 500)
    
            self.m_animation = QtCore.QPropertyAnimation(
                self.text2, b"maximumWidth", parent=self, duration=250
            )
    
        def closeOpenEditor(self):
            if self.btn.isChecked():
                self.text2.setMaximumWidth(self.text2.width())
                text2Start = int(self.text2.maximumWidth())
                self.m_deltaX = text2Start
                text2End = 3
                self.m_animation.setStartValue(text2Start)
                self.m_animation.setEndValue(text2End)
                self.btn.setText("<")
            else:
                text2Start = int(self.text2.maximumWidth())
                text2End = self.m_deltaX
                self.m_animation.setStartValue(text2Start)
                self.m_animation.setEndValue(text2End)
                self.btn.setText(">")
    
            self.m_animation.start()
    
        def resizeEvent(self, event: "QResizeEvent"):
            if not self.btn.isChecked():
                self.text2.setMaximumWidth(QWIDGETSIZE_MAX)
    
    
    if __name__ == "__main__":
        import sys
    
        app = QtWidgets.QApplication(sys.argv)
        w = MyWidget()
        w.show()
        sys.exit(app.exec_())
    

    【讨论】:

      猜你喜欢
      • 2014-10-19
      • 2016-05-18
      • 2015-06-01
      • 1970-01-01
      • 1970-01-01
      • 2022-08-17
      • 1970-01-01
      • 1970-01-01
      • 2022-07-28
      相关资源
      最近更新 更多