Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/qt/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Qt 将QStackedLayout动态转换为QHBoxLayout_Qt_Layout - Fatal编程技术网

Qt 将QStackedLayout动态转换为QHBoxLayout

Qt 将QStackedLayout动态转换为QHBoxLayout,qt,layout,Qt,Layout,我想通过单击按钮动态地将我的小部件布局从QStackedLayout更改为QHBoxLayout,再更改为QVBoxLayout。我可以从QVBoxLayout切换到QHBoxLayout,反之亦然,但我的方法不适用于QStackedLayout。我已经用尽了我能想到的所有选择。随附示例代码。有人知道我如何实现我的目标吗 #ifndef WIDGET_H #define WIDGET_H #include <QWidget> #include <QPushButton>

我想通过单击按钮动态地将我的小部件布局从QStackedLayout更改为QHBoxLayout,再更改为QVBoxLayout。我可以从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;
}

从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_);
还值得注意的是,在删除之前从布局中删除小部件是没有必要的。布局不拥有其中的小部件的所有权。因此,您可以删除以下内容:

void Widget::layoutHorizontal() {

   ...

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

   isStackedLayout_ = false;
}
layout->removeWidget(button1_);
layout->removeWidget(button2_);
layout->removeWidget(button3_);

创建另一个小部件,其中只有一个小部件和一个布局。然后对小部件使用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);
    }    
}