2010-07-19 76 views
0

说我有一个QHBoxLayout那里有2个QTextEdit S和它们之间的按钮有一个箭头向右。当你点击按钮时,右边的QTextEdit通过移动左边框逐渐关闭,直到它遇到正确的边框。同时,左边QTextEdit的右边框取右边QTextEdit发布的地方。按下按钮后,系统的状态即将到达前一个状态。QWidget的 - 调整动画

编辑:为了组织本我也做了以下内容:

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恢复其状态(但应保持关闭状态,当然)。我怎么解决这个问题?

请为我提供一段代码。

回答

1

在这里我想要的东西:

头文件

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

也许你的解决方案的描述,而不是高度特定于你的情况代码,对于其他人在SO上会更有帮助 – Troyseph 2015-04-15 13:41:12

3

Qt's Animation framework听起来像一个良好的开端。你可以试着按照他们的教程,适应你的使用案例。我已经用过了,而且看起来非常直截了当。

+0

我已阅读并例子混熟。不幸的是,我的经验并没有让我执行这种情况。 – Narek 2010-07-19 18:58:29

+0

@Narek为什么?你能详细说明吗?有什么问题? – Gianni 2010-07-19 19:23:54

+0

好的,我改变了我的问题。请参阅上文。 – Narek 2010-07-20 10:05:01

3

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)我想你可以(也应该)动画控件的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); 
    ... 
} 

而且,现在你不必动画按钮,在所有几何体(假设你有固定的大小设置为它)。

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

+0

如何删除按钮和正确的QTextEdit(它最初存在于QHBoxLayout中)之间的距离? – Narek 2010-07-20 12:16:50

+0

您的解决第二个问题的逻辑很好,但这是一个小问题:稍等一会,然后立即关闭第二个QTextEdit。当我添加animation1-> setDuration(3000);它等待更多,然后,同样的行为,imediatley关闭。我们如何才能逐渐做到呢? – Narek 2010-07-20 12:34:16

+0

您可以使用QBoxLayout :: setStretchFactor来摆脱布局中元素之间的空隙(即,为每个小部件设置拉伸因子为零)。对于另一个问题,你也可以尝试动画minimumWidth。 – Darqan 2010-07-20 13:20:39