python - 如何修复 QPropertyAnimation "starting an animation without end value"错误

标签 python c++ qt pyside2

我正在尝试将 qt 示例从 C++ 移植到 pyqt/pyside2。此示例使用 QPropertyAnimation 显示带有文本编辑字段的隐藏/显示操作。

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

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

这是我的 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++ 代码:

主窗口.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

main.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, ): starting an animation without end value

NameError: name 'QWIDGETSIZE_MAX' is not defined

最佳答案

您的代码有以下错误:

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

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

  • 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_())

关于python - 如何修复 QPropertyAnimation "starting an animation without end value"错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56654339/

相关文章:

python - 带有 apache ldap 后端身份验证的 django,登录用户名和 ldap 组并隐藏基于组的详细信息 (mod_ldap)

c++ - 如果容器不是调用函数中的引用,则使用 std::thread 传递对迭代器的引用失败

c++ - 在没有上下文参数的情况下将有状态 lambda 传递给 C 样式函数

c++ - QSettings 在没有管理员权限的情况下不保存 ini 更改

c++ - QString 到 char 不工作

javascript - 如何将这段 Python 代码翻译成 JavaScript? (涉及集合)

python - python会重用重复的计算结果吗?

python - "UnicodeDecodeError: ' charmap ' codec can' t decode"pickle 加载错误

c++ - 使用 WritePrinter API 将中文字符打印到行式打印机

python - python中递归收集所有选中的QTreeview项