2013-08-01 76 views
0

我想通過單擊按鈕動態地將我的小部件佈局從QStackedLayout更改爲QHBoxLayoutQVBoxLayout。我能夠從QVBoxLayout切換到QHBoxLayout,反之亦然,但我的方法不適用於QStackedLayout。我用盡了所有我能想到的選項。附上示例代碼。有誰知道我可以如何實現我的目標?動態地將QStackedLayout轉換爲QHBoxLayout

#ifndef WIDGET_H 
#define WIDGET_H 

#include <QWidget> 
#include <QPushButton> 
#include <QHBoxLayout> 
#include <QVBoxLayout> 
#include <QStackedLayout> 
#include <QGridLayout> 
#include <QLabel> 

class Widget : public QWidget 
{ 
    Q_OBJECT 

public: 
    Widget(QWidget *parent = 0); 
    ~Widget(); 
    QPushButton *button1_; 
    QPushButton *button2_; 
    QPushButton *button3_; 

    QHBoxLayout *hLayout_; 
    QVBoxLayout *vLayout_; 
    QStackedLayout *sLayout_; 
    QVBoxLayout *gLayout_; 

public slots: 
    void layoutHorizontal(); 
    void layoutVertical(); 
    void layoutStacked(); 

private: 
    bool isStackedLayout_; 
    QLabel *bar_; 
}; 

#endif // WIDGET_H 


#include "widget.h" 
#include <QtAlgorithms> 
#include <QDebug> 

Widget::Widget(QWidget *parent) : QWidget(parent){ 

    bar_ = new QLabel(tr("TEST!")); 

    button1_ = new QPushButton(tr("to Horizontal Layout"),(bar_)); 
    button2_ = new QPushButton(tr("to Vertical Layout"),(bar_)); 
    button3_ = new QPushButton(tr("to Stacked Layout"),(bar_)); 

    button1_->setStyleSheet("background: rgba(255,255,0,255);"); 
    button2_->setStyleSheet("background: rgba(255,0,255,255);"); 
    button3_->setStyleSheet("background: rgba(0,255,255,255);"); 

    connect(button1_,SIGNAL(clicked()),this,SLOT(layoutHorizontal())); 
    connect(button2_,SIGNAL(clicked()),this,SLOT(layoutVertical())); 
    connect(button3_,SIGNAL(clicked()),this,SLOT(layoutStacked())); 


    gLayout_ = new QVBoxLayout; 
    setLayout(gLayout_); 

    hLayout_ = new QHBoxLayout(bar_); 
    hLayout_->setObjectName(tr("currentLayout")); 
    gLayout_->addWidget(bar_); 

    hLayout_->addWidget(button1_); 
    hLayout_->addWidget(button2_); 
    hLayout_->addWidget(button3_); 

    isStackedLayout_ = false; 

    resize(480,200); 

} 

Widget::~Widget() { } 

void Widget::layoutHorizontal(){ 
    QLayout *layout = bar_->findChild<QLayout *>(tr("currentLayout")); 
    layout->removeWidget(button1_); 
    layout->removeWidget(button2_); 
    layout->removeWidget(button3_); 

    delete layout; 

    QHBoxLayout *hLayout_ = new QHBoxLayout(bar_); 
    hLayout_->setObjectName(tr("currentLayout")); 
    hLayout_->addWidget(button1_); 
    hLayout_->addWidget(button2_); 
    hLayout_->addWidget(button3_); 

    isStackedLayout_ = false; 

} 

void Widget::layoutVertical(){ 
    QLayout *layout = bar_->findChild<QLayout *>(tr("currentLayout")); 
    layout->removeWidget(button1_); 
    layout->removeWidget(button2_); 
    layout->removeWidget(button3_); 
    delete layout; 

    QVBoxLayout *vLayout_ = new QVBoxLayout(bar_); 
    vLayout_->setObjectName(tr("currentLayout")); 

    vLayout_->addWidget(button1_); 
    vLayout_->addWidget(button2_); 
    vLayout_->addWidget(button3_); 

    isStackedLayout_ = false; 
} 

void Widget::layoutStacked(){ 
    QLayout *layout = bar_->findChild<QLayout *>(tr("currentLayout")); 
    layout->removeWidget(button1_); 
    layout->removeWidget(button2_); 
    layout->removeWidget(button3_); 
    delete layout; 

    QStackedLayout *sLayout_ = new QStackedLayout(bar_); 
    sLayout_->setObjectName(tr("currentLayout")); 

    sLayout_->addWidget(button1_); 
    sLayout_->addWidget(button2_); 
    sLayout_->addWidget(button3_); 

    isStackedLayout_ = true; 
} 

回答

0

visibility屬性上的按鈕被拉出部件出QStackedLayout(可能是因爲QStackedLayout依賴於該屬性的模擬堆疊)後,設置爲「不可見」。因此,我能得到你的代碼中加入工作如下:

void Widget::layoutHorizontal() { 

    ... 

    button1_->setVisible(true); 
    button2_->setVisible(true); 
    button3_->setVisible(true); 

    isStackedLayout_ = false; 
} 

這也是值得注意的是,從佈局刪除它之前去除小部件是沒有必要的。該佈局不承擔其中的小部件的所有權。因此,您可以刪除以下內容:

layout->removeWidget(button1_); 
layout->removeWidget(button2_); 
layout->removeWidget(button3_); 
+0

非常感謝。有效。 – user2643538

0

創建另一個只有小部件和一個佈局的小部件。然後使用setLayout爲您的小部件。對不起,我的C++知識不佳:)

class ChangeableWidget: public QWidget { 

    private QVBoxLayout vbox; 
    private QHBoxLayout hbox; 
    private QStackedLayout stacked; 

    private QPushButton button1; 
    private QPushButton button2; 

    ChangeableWidget() 
    { 
     button1 = new QPushButton("1"); 
     button2 = new QPushButton("2"); 

     vbox = new QVBoxLayout(); 
     vbox.addWidget(button1); 
     vbox.addWidget(button2); 

     hbox = new QHBoxLayout(); 
     hbox.addWidget(button1); 
     hbox.addWidget(button2);   

     stacked = new QStackedLayout(); 
     stacked.addWidget(button1); 
     stacked.addWidget(button2); 

     setLayout(vbox); 
    } 

    void ChangeableWidget::layoutVertical() 
    { 
     setLayout(vbox); 
    } 

    void ChangeableWidget::layoutHorizontal() 
    { 
     setLayout(hbox); 
    }  
}