2013年8月30日 星期五

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

顯示結果:

沒有留言:

張貼留言