2010-07-19 73 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