分类: QT2012-07-25 21:59 6997人阅读
堆栈窗口可以根据选择项的不同显示不同的窗体


- #ifndef STACKEDDLG_H
- #define STACKEDDLG_H
-
- #include <QDialog>
- #include <QLabel>
- #include <QListWidget>
- #include <QStackedWidget>
-
- class StackedDlg : public QDialog
- {
- Q_OBJECT
- public:
- explicit StackedDlg(QWidget *parent = 0);
- signals:
- public slots:
-
- private:
- QLabel *textLabel;
- QLabel *showLabel;
- QLabel *aboutLabel;
- QListWidget *list;
- QStackedWidget *stack;
- };
- #endif // STACKEDDLG_H
- #include "stackeddlg.h"
- #include <QHBoxLayout>
-
- StackedDlg::StackedDlg(QWidget *parent) :
- QDialog(parent)
- {
-
- list = new QListWidget();
- list->insertItem(0, tr("文本窗口"));
- list->insertItem(1, tr("显示窗口"));
- list->insertItem(2, tr("关于窗口"));
-
- textLabel = new QLabel(tr("学习使用堆栈窗口"));
- showLabel = new QLabel(tr("仅仅是一个示例程序"));
- aboutLabel = new QLabel(tr("欢迎交流,共同学习"));
-
-
- stack = new QStackedWidget();
-
- stack->addWidget(textLabel);
- stack->addWidget(showLabel);
- stack->addWidget(aboutLabel);
-
-
- QHBoxLayout *mainLayout = new QHBoxLayout(this);
- mainLayout->addWidget(list);
- mainLayout->addWidget(stack, 0, Qt::AlignHCenter);
-
- mainLayout->setMargin(5);
- mainLayout->setSpacing(5);
-
-
- connect(list, SIGNAL(currentRowChanged(int)), stack, SLOT(setCurrentIndex(int)));
-
- this->setWindowTitle(tr("堆栈窗口"));
- }
- #include <QApplication>
- #include <QTextCodec>
- #include "stackeddlg.h"
- int main(int argc, char *argv[])
- {
- QTextCodec::setCodecForTr(QTextCodec::codecForLocale());
- QApplication app(argc, argv);
- StackedDlg dlg;
- dlg.show();
- return app.exec();
- }
Qt使用QStackedWidget实现堆栈窗口
原文:http://www.cnblogs.com/584709796-qq-com/p/4968880.html