Qt - There is a bug in QPropertyAnimation?

后端 未结 4 934
时光取名叫无心
时光取名叫无心 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:14

    Did you check the actual value of maximumWidth? You don't seem to set a specific maximumWidth in your code.

    The default value for maximumWidth is 16777215, and you set a duration of 1 msec. for the closing animation. Fading from 16777215 to 3 in 1 msec. would look like "instant", I guess.

    0 讨论(0)
  • 2021-01-26 11:17

    This is not a bug, the response you got from the bug report pretty well explains the problem with your code and a solution.

    0 讨论(0)
  • 2021-01-26 11:21

    I don't think it is a bug; I'd call it "undefined behavior". That means that if you try to animate minimumWidth, nobody can tell you for sure what is supposed to happen, and maybe the code has some optimizations or corner cases where sometimes it works, others it doesn't.

    Anyway, minimumWidth and maximumWidth aren't supposed to be used to define what the size of a QWidget is, only what it must not exceed; i.e. they weren't designed to do what you are trying to do, so it can be called a bug. If you want to animate, you have to use a deterministic approach, which in this case is using the geometry property.

    0 讨论(0)
  • 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 );
    }
    
    0 讨论(0)
提交回复
热议问题