c++ - QWidget - 调整大小动画

标签 c++ qt animation qwidget

假设我有一个 QHBoxLayout,其中有 2 个 QTextEdit,它们之间有一个带向右箭头的按钮。当您单击该按钮时,右侧的 QTextEdit 通过移动左边框直到与右边框相交逐渐关闭。同时,左侧QTextEdit 的右边框取代了右侧QTextEdit 释放的位置。而按下按钮后,系统状态又回到了之前的状态。

编辑:为了组织这个,我做了以下工作:

1) 在头文件中:

class MyWidget : public QWidget
{

    Q_OBJECT

    QTextEdit       *m_textEditor1;
    QTextEdit       *m_textEditor2;
    QPushButton     *m_pushButton;
    QHBoxLayout     *m_layout;
    int              m_deltaX;

public:

    MyWidget(QWidget * parent = 0);


    ~MyWidget(){}


private slots:
    void closeOrOpenTextEdit2(bool isClosing);


};

2) 在源文件中:

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


  m_pushButton = new QPushButton(this);
  m_pushButton->setText(">");
  m_pushButton->setCheckable(true);
  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_layout = new QHBoxLayout;
  m_layout->addWidget(m_textEditor1);
  m_layout->addWidget(m_pushButton);
  m_layout->addWidget(m_textEditor2);

  setLayout(m_layout);
}

void MyWidget::closeOrOpenTextEdit2(bool isClosing)
{
    QPropertyAnimation *animation1 = new QPropertyAnimation(m_textEditor2, "geometry");
    QPropertyAnimation *animation2 = new QPropertyAnimation(m_pushButton, "geometry");
    QPropertyAnimation *animation3 = new QPropertyAnimation(m_textEditor1, "geometry");

    if(isClosing) //close the second textEdit
    {
        m_pushButton->setText("<");

        QRect te2_1 = m_textEditor2->geometry();
        m_deltaX = te2_1.width()-3;
        QRect te2_2(te2_1.x()+m_deltaX, te2_1.y(), 3 ,te2_1.height());

        QRect pb_1 = m_pushButton->geometry();
        QRect pb_2(pb_1.x()+m_deltaX, pb_1.y(), pb_1.width() ,pb_1.height());

        QRect te1_1 = m_textEditor1->geometry();
        QRect te1_2(te1_1.x(), te1_1.y(), te1_1.width()+m_deltaX, te1_1.height());


         //animation->setDuration(10000);
         animation1->setStartValue(te2_1);
         animation1->setEndValue(te2_2);

         animation2->setStartValue(pb_1);
         animation2->setEndValue(pb_2);

         animation3->setStartValue(te1_1);
         animation3->setEndValue(te1_2);
    }
    else //open
    {
       m_pushButton->setText(">");

       QRect te2_1 = m_textEditor2->geometry();
       QRect te2_2(te2_1.x()-m_deltaX, te2_1.y(), 3+m_deltaX ,te2_1.height());

       QRect pb_1 = m_pushButton->geometry();
       QRect pb_2(pb_1.x()-m_deltaX, pb_1.y(), pb_1.width() ,pb_1.height());

       QRect te1_1 = m_textEditor1->geometry();
       QRect te1_2(te1_1.x(), te1_1.y(), te1_1.width()-m_deltaX, te1_1.height());


        //animation->setDuration(10000);
        animation1->setStartValue(te2_1);
        animation1->setEndValue(te2_2);

        animation2->setStartValue(pb_1);
        animation2->setEndValue(pb_2);

        animation3->setStartValue(te1_1);
        animation3->setEndValue(te1_2);

    }
    animation1->start();
    animation2->start();
    animation3->start();
}

编辑:

我有以下问题:

当我关闭第二个 QTextEdit(通过单击按钮)并调整 MyWidget 的大小时,QTextEdit 恢复其状态(但是它当然应该保持关闭状态)。我该如何解决这个问题?

请提供代码片段给我。

最佳答案

Qt's Animation framework听起来是个不错的起点。您可以尝试按照他们的教程进行操作,以适应您的用例。我已经使用过它,它看起来非常简单。

关于c++ - QWidget - 调整大小动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3283587/

相关文章:

animation - Alive GUI 库,支持 Haskell 的 FRP

c++ - 激发 'inline' 说明符的现实世界示例?

c++ - 我可以将 Visual Studio 2005 设置为在调试时忽略特定代码区域中的断言吗

c++ - 如何使用 Arnold API 实现兰伯特着色器?

google-chrome - Chrome 动画期间损坏的标记 (jsfiddle)

Swift - View Controller 之间向前和向后的动画

c++ - 海湾合作委员会 : Unscoped enumeration type give an ambiguity error

python - PyQt - QFileDialog - 直接浏览到文件夹?

Qt双免费或损坏

qt - WebView显示空白白屏