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

顯示結果:


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

範例1:增加按鈕(Push Button),格子排版

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)
{
    QPushButton *button1, *button2, *button3, *button4;
    button1 = new QPushButton(tr("1"));
    button2 = new QPushButton(tr("2"));
    button3 = new QPushButton(tr("3"));
    button4 = new QPushButton(tr("4"));

    QGridLayout *layout = new QGridLayout;
    layout->addWidget(button1, 0, 0);
    layout->addWidget(button2, 0, 1);
    layout->addWidget(button3, 1, 0);
    layout->addWidget(button4, 1, 1);

    setLayout(layout);

}

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

顯示結果:

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

範例1:增加按鈕(Push Button),垂直排版

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)
{
    QPushButton *button1, *button2;
    button1 = new QPushButton(tr("YES"));
    button2 = new QPushButton(tr("NO"));

    QVBoxLayout *layout = new QVBoxLayout;
    layout->addWidget(button1);
    layout->addWidget(button2);

    setLayout(layout);
}

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

顯示結果:

2013年8月29日 星期四

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

Qt設計視窗的版面規劃有兩種方式,一種是Qt設計器(Qt Designer)規劃,另外一種就是Qt版面配置管理器(QLayout),我在這裡紀錄一下使用版面配置管理器程式範例.
範例1:增加按鈕(Push Button),水平排版

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)
{
    QPushButton *button1, *button2;
    button1 = new QPushButton(tr("YES"));
    button2 = new QPushButton(tr("NO"));

    QHBoxLayout *layout = new QHBoxLayout;
    layout->addWidget(button1);
    layout->addWidget(button2);

    setLayout(layout);

}

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

顯示結果: