1

我想通过单击按钮将我的小部件布局从动态QStackedLayout更改QHBoxLayout为动态。QVBoxLayout我可以从 to 切换,QVBoxLayout反之亦然QHBoxLayout,但我的方法不适用于QStackedLayout. 我已经用尽了所有我能想到的选择。附上示例代码。有谁知道我怎样才能实现我的目标?

#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;
}
4

2 回答 2

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);
    }    
}
于 2013-08-02T03:21:45.557 回答
0

将小部件拉出后,按钮上的可见性属性设置为“不可见” 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_);
于 2013-08-02T00:49:50.553 回答