Qt - There is a bug in QPropertyAnimation?

后端 未结 4 965
时光取名叫无心
时光取名叫无心 2021-01-26 10:36

I face a very serious situation. By writing this question I hope that really professionals will express their opinion regarding to the problem I am going to describe. I have rep

4条回答
  •  情深已故
    2021-01-26 11:24

    Dear Sofahamster I have changed my code to the code below and it works fine. Thanks for your hint!

    Header file

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

    Source file

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

提交回复
热议问题