代码之家  ›  专栏  ›  技术社区  ›  Narek

Qt-QPropertyAnimation中存在错误?

  •  0
  • Narek  · 技术社区  · 14 年前

    https://bugreports.qt.io/ :

    我已经为QTextEdit的maximumWidth属性创建了QPropertyAnimation,但它不起作用(它会立即将状态从开始状态更改为结束状态),尽管它适用于minimumWidth属性。 请参阅所附代码。

    并附有.h和.cpp文件。看到那些文件了吗 here

    另外,我之所以编写这段代码,是因为我想通过动画关闭右qtexted,并确保在调整主窗口(按钮和两个qtexted所在的位置)的大小时,关闭的qtexted不会被还原。

    4 回复  |  直到 8 年前
        1
  •  3
  •   Sofahamster    14 年前

    maximumWidth的默认值是16777215,您将持续时间设置为1毫秒。对于结束动画。在1毫秒内从16777215衰减到3。我想看起来像“瞬间”。

        2
  •  2
  •   Gianni    14 年前

    无论如何,minimumWidth和maximumWidth不应该用来定义QWidget的大小,只应该定义它不能超过的大小;i、 它们并不是设计用来做你想做的事情,所以它可以被称为bug。如果要设置动画,必须使用确定性方法,在本例中使用的是“几何体”属性。

        3
  •  0
  •   bimbom22    14 年前

    这不是一个bug,从bug报告中得到的响应很好地解释了代码和解决方案的问题。

        4
  •  0
  •   Narek    14 年前

    亲爱的Sofahamster,我已经把我的代码改为下面的代码,它可以正常工作。谢谢你的提示!

    头文件

    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);
    
    };
    

    源文件

    MyWidget::MyWidget(QWidget * parent):QWidget(parent),m_deltaX(0)
    {
    
      m_pushButton = new QPushButton(this);
      m_pushButton->setText(">");
      m_pushButton->setCheckable(true);
      m_pushButton->setFixedSize(16,16);
      connect(m_pushButton, SIGNAL(clicked(bool)), this, SLOT(closeOrOpenTextEdit2(bool)));
    
      m_textEditor1 = new QTextEdit(this);
      m_textEditor1->setText("AAAAA AAAAAAAAAAA AAAAAAAAAAA  AAAAAAA AAAAAAAAAAA AAAAAAAAAAA  AA");
    
      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(500);
            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(500);
            animation1->setStartValue(textEdit2_start);
            animation1->setEndValue(textEdit2_end);
    
    
            m_pushButton->setText(">");
    
        }
    
        animation1->start();
    
    }
    
    
    void MyWidget::resizeEvent( QResizeEvent * event )
    {
        if(!m_isClosed)
            m_textEditor2->setMaximumWidth( QWIDGETSIZE_MAX );
    }