2013年8月30日 星期五

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

顯示結果:

沒有留言:

張貼留言