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

QWidget-调整动画大小

  •  1
  • Narek  · 技术社区  · 15 年前

    说我有一个 QHBoxLayout 哪里有2个 QTextEdit 在它们之间有一个向右箭头的按钮。当你点击按钮时,右边 Q文本编辑 通过移动左边界逐渐闭合,直到它遇到右边界。同时,左边的右边框 Q文本编辑 Q文本编辑 释放。按下按钮后,系统状态恢复到原来的状态。

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

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

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

    编辑:

    我有以下问题:

    Q文本编辑 (通过单击按钮)并调整 MyWidget Q文本编辑 恢复其状态(当然,它应该保持关闭状态)。我怎样才能解决这个问题?

    请给我一个代码片段。

    3 回复  |  直到 11 年前
        1
  •  3
  •   Gianni    15 年前
        2
  •  3
  •   Darqan    15 年前

    1) 您可以将按钮替换为垂直布局,将按钮放置在此布局中,最后在按钮下方添加一个垂直间隔符(在同一布局中)。

    ...
    
    QVBoxLayout* m_buttonLayout = new QVBoxLayout();
    
    m_layout = new QHBoxLayout();
    m_layout->addWidget(m_textEditor1);
    m_layout->addLayout(m_buttonLayout);
    m_layout->addWidget(m_textEditor2);
    
    m_buttonLayout->addWidget(m_pushButton);
    m_buttonLayout->addItem( new QSpacerItem(1, 1, QSizePolicy::Minimum, QSizePolicy::Expanding) );
    

    2) 我想你可以(而且应该)设置widget的maximumSize(或者只是maximumWidth)属性的动画,让布局负责计算实际的几何图形。这也会简化你的计算。例如。

    QPropertyAnimation *animation1 = new QPropertyAnimation(m_textEditor2, "maximumWidth");
    QPropertyAnimation *animation2 = new QPropertyAnimation(m_textEditor, "maximumWidth");
    
    if (isClosing)
    {
        int textEdit2_start = m_textEditor2->maximumWidth();
        int textEdit2_end = 0;
    
        int textEdit_start = m_textEditor->maximumWidth();
        int textEdit_end = textEdit_start + textEdit2_start;
    
        animation1->setStartValue(textEdit2_start);
        ...
    }
    

    另外,现在您根本不必为按钮几何体设置动画(假设已将其设置为固定大小)。

    另外,我没有编译代码,所以可能会有一些小错误,但你应该明白。

        3
  •  2
  •   Narek    15 年前

    我想要的是:

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