2013年8月30日 星期五

Qt版面配置管理(QLayout)-4

範例1:增加下拉式選單(Combo Box),堆疊排版

1. Helloworld.pro


TARGET = helloworld

SOURCES += main.cpp \
    layoutdlg.cpp

HEADERS  += \
    layoutdlg.h


FORMS    += dialog.ui


2. main.cpp


#include <QtGui>
#include <QtCore>

#include "layoutdlg.h"
int main(int argc, char* argv[])
{
    QApplication app(argc, argv);

    layoutdlg helloworld;
    helloworld.show();

    return app.exec();
}

3. layoutdlg.cpp


#include <QtGui>
#include "layoutdlg.h"

layoutdlg::layoutdlg(QDialog *parent)
 : QDialog(parent)
{
    QLabel *label1 = new QLabel("Lable1");
    QWidget *pageWidget1 = new QWidget;
    QVBoxLayout *layout1 = new QVBoxLayout();
    layout1->addWidget(label1);
    pageWidget1->setLayout(layout1);


    QLabel *label2 = new QLabel("Lable2");
    QWidget *pageWidget2 = new QWidget;
    QVBoxLayout *layout2 = new QVBoxLayout();
    layout2->addWidget(label2);
    pageWidget2->setLayout(layout2);


    QStackedLayout *stackedLayout = new QStackedLayout;
    stackedLayout->addWidget(pageWidget1);
    stackedLayout->addWidget(pageWidget2);


    QComboBox *pageComboBox = new QComboBox;
    pageComboBox->addItem(tr("Set Lable1"));
    pageComboBox->addItem(tr("Set Lable2"));
    connect(pageComboBox, SIGNAL(activated(int)),
            stackedLayout, SLOT(setCurrentIndex(int)));


    QVBoxLayout *mainLayout = new QVBoxLayout;
    mainLayout->addWidget(pageComboBox);
    mainLayout->addLayout(stackedLayout);
    setLayout(mainLayout);
}


4. layoutdlg.h

#ifndef LAYOUTDLG_H
#define LAYOUTDLG_H


#include <QDialog>

class QPushButton;

class layoutdlg  :  public QDialog
{
    Q_OBJECT
public:
    layoutdlg(QDialog *parent = 0);
};


#endif // LAYOUTDLG_H

顯示結果:


沒有留言:

張貼留言