1

我正在测试如何在StackedLayout按下特定按钮时使用 来更改屏幕上的布局。

测试.h

#ifndef TEST_H
#define TEST_H

#include <QLabel>
#include <QApplication>
#include <QLabel>
#include <QPushButton>
#include <QHBoxLayout>
#include <QStackedLayout>
#include <QVBoxLayout>
#include <QComboBox>

class Test: public QWidget{
    Q_OBJECT

private:
    QPushButton *button1, *button2;
    QWidget *parentWidget1, *parentWidget2;
    QLabel *label1, *label2;
    QHBoxLayout *layout1, *layout2;
    QStackedLayout *stackedLayout;
    QHBoxLayout *mainLayout;

private slots:
    void layout1_h();
    void layout2_h();

public:
    Test(QWidget* parent=0);

};


#endif

测试.cpp

#include "test.h"
using namespace std;

void Test::layout1_h()
{
    stackedLayout->setCurrentIndex(0);
    //window->show();
}

void Test::layout2_h()
{
    stackedLayout->setCurrentIndex(1);
    //window->show();
}



Test::Test(QWidget *parent):QWidget(parent)
{
    //widget = new QWidget;
    stackedLayout = new QStackedLayout;
    parentWidget1 = new QWidget;
    parentWidget2 = new QWidget;
    button1 = new QPushButton("change1");
    button2 = new QPushButton("change2");
    label1 = new QLabel("hello1");
    label2 = new QLabel("hello2");
    layout1 = new QHBoxLayout;
    layout2 = new QHBoxLayout;

    layout1->addWidget(label1);
    layout1->addWidget(button1);
    layout1->addWidget(button2);

    layout2->addWidget(label2);
    layout2->addWidget(button1);
    layout2->addWidget(button2);

    parentWidget1->setLayout(layout1);
    parentWidget2->setLayout(layout2);
    stackedLayout->addWidget(parentWidget1);
    stackedLayout->addWidget(parentWidget2);
    stackedLayout->setCurrentIndex(0);
    mainLayout = new QHBoxLayout;
    mainLayout->addLayout(stackedLayout);

    connect(button1, SIGNAL(clicked()), this, SLOT(layout1_h()));
    connect(button2, SIGNAL(clicked()), this, SLOT(layout2_h()));

    setLayout(mainLayout);
    //window.show();

}

所以我启动并运行了应用程序,但由于某种原因,没有显示按钮。怎么了??

4

1 回答 1

2

button1 和 button2 不能同时在两个布局中。layout2 成为两个按钮的管理器,而 parentWidget2 成为它们的所有者。

于 2013-03-06T11:15:15.583 回答