您的位置:首页 > 编程语言 > Qt开发

QT学习 第二章:布局管理--基本布局管理

2011-05-30 15:35 561 查看
QT学习 第二章:布局管理--基本布局管理

效果截图:



创建文件夹LayoutDlg,编写三个源码文件:

/** Object: LayoutDlg
** Desc:   基本布局管理
** File:   main.cpp
** Compile:qmake-qt4 -project;qmake-qt4;make;
** Author: LiXiujie www.xiujie.cn
** Date:  2011-05-30
** Note:  编译说明:
**      qmake-qt4 -prject 自动生成程序的项目文件(*.pro);
**      qmake-qt4 用于生成程序的Makefile文件;
**      make 编译 Makefile 文件得到可执行文件。
** */
#include <QApplication> // 所有QT图形化应用程序必须包含此文件,它包含了QT图形化应用程序的各种资源、基本设置、控制流及事件处理等。
#include "LayoutDlg.h" // 自定义类头文件
int main(int argc, char *argv[]){
QApplication app(argc, argv);
LayoutDlg *pDlg = new LayoutDlg();
pDlg->show();
return app.exec();
}


/** Object: LayoutDlg
** Desc:   基本布局管理
** File:   LayoutDlg.h
** Class:  LayoutDlg
** Compile:qmake-qt4 -project;qmake-qt4;make;
** Author: LiXiujie www.xiujie.cn
** Date:  2011-05-30
** Note:  编译说明:
**      qmake-qt4 -prject 自动生成程序的项目文件(*.pro);
**      qmake-qt4 用于生成程序的Makefile文件;
**      make 编译 Makefile 文件得到可执行文件。
** */
#ifndef LAYOUTDLG_H
#define LAYOUTDLG_H
#include <QtGui>
class LayoutDlg : public QDialog
{
Q_OBJECT
public:
LayoutDlg(QWidget *parent=0, Qt::WindowFlags f=0);
QLabel *m_pLabel_1; // 标签框
QLabel *m_pLabel_2;
QLabel *m_pLabel_3;
QLabel *m_pLabel_4;
QLabel *m_pLabel_5;
QLabel *m_pLabel_6;
QLabel *m_pLabel_7;
QLabel *m_pLabel_Other;
QLabel *m_pLabel_Icon;
QLineEdit *m_pLE_User; // 单行文本框
QLineEdit *m_pLE_Name;
QComboBox *m_pCB_Sex; // 下拉列表框
QTextEdit *m_pTE_Department;// 文本框
QSpinBox  *m_pSB_Age;  // 数字增减框
QTextEdit *m_pTE_Description;
QPushButton *m_pPB_Icon;
QPushButton *m_pPB_OK;
QPushButton *m_pPB_Exit;
};
#endif // LAYOUTDLG_H


/** Object: LayoutDlg
** Desc:   基本布局管理
** File:   LayoutDlg.cpp
** Class:  LayoutDlg
** Compile:qmake-qt4 -project;qmake-qt4;make;
** Author: LiXiujie www.xiujie.cn
** Date:  2011-05-30
** Note:  编译说明:
**      qmake-qt4 -prject 自动生成程序的项目文件(*.pro);
**      qmake-qt4 用于生成程序的Makefile文件;
**      make 编译 Makefile 文件得到可执行文件。
** */
#include "LayoutDlg.h"
LayoutDlg::LayoutDlg(QWidget *parent, Qt::WindowFlags f):QDialog(parent, f)
{
setWindowTitle(tr("User Infomation")); // 设置
m_pLabel_1 = new QLabel(tr("User Name:"));
m_pLabel_2 = new QLabel(tr("Name:"));
m_pLabel_3 = new QLabel(tr("Sex:"));
m_pLabel_4 = new QLabel(tr("Department:"));
m_pLabel_5 = new QLabel(tr("Age:"));
m_pLabel_Other = new QLabel(tr("Remark:"));
m_pLabel_Other->setFrameStyle(QFrame::Panel|QFrame::Sunken); // 面板,凹陷
m_pLE_User = new QLineEdit;
m_pLE_Name = new QLineEdit;
m_pCB_Sex = new QComboBox;
m_pCB_Sex->insertItem(0, tr("Female"));
m_pCB_Sex->insertItem(1, tr("Male"));
m_pTE_Department = new QTextEdit;
m_pSB_Age = new QSpinBox;
m_pSB_Age->setMinimum(0);
m_pSB_Age->setMaximum(200);
QGridLayout *pGLayout_Left = new QGridLayout(); // 表格布局容器
pGLayout_Left->addWidget(m_pLabel_1, 0, 0); // 第一行,第一列中添加控件
pGLayout_Left->addWidget(m_pLE_User, 0, 1);
pGLayout_Left->addWidget(m_pLabel_2, 1, 0);
pGLayout_Left->addWidget(m_pLE_Name, 1, 1); // 第二行,第二列中添加控件
pGLayout_Left->addWidget(m_pLabel_3, 2, 0);
pGLayout_Left->addWidget(m_pCB_Sex, 2, 1);
pGLayout_Left->addWidget(m_pLabel_4, 3, 0, Qt::AlignTop); // 垂直顶端对齐
pGLayout_Left->addWidget(m_pTE_Department, 3, 1);
pGLayout_Left->addWidget(m_pLabel_5, 4, 0);
pGLayout_Left->addWidget(m_pSB_Age, 4, 1);
pGLayout_Left->addWidget(m_pLabel_Other, 5, 0, 1, 2); // 第六行,第一列中添加控件,占一行两列
pGLayout_Left->setColumnStretch(0, 1);
pGLayout_Left->setColumnStretch(1, 3); // 第一列和第两列的宽度比例为1:3
m_pLabel_7 = new QLabel(tr("Head:"));
m_pLabel_Icon = new QLabel;
QPixmap icon("icon.png");
m_pLabel_Icon->resize(icon.width(), icon.height());
m_pLabel_Icon->setPixmap(icon);
m_pPB_Icon = new QPushButton();
m_pPB_Icon->setText(tr("Change"));
QHBoxLayout *pHBLayout = new QHBoxLayout; // 水平布局容器
pHBLayout->setSpacing(20); // 间距
pHBLayout->addWidget(m_pLabel_7);
pHBLayout->addWidget(m_pLabel_Icon);
pHBLayout->addWidget(m_pPB_Icon);
m_pLabel_6 = new QLabel(tr("Individual:"));
m_pTE_Description = new QTextEdit;
QVBoxLayout *pVBLayout_Right = new QVBoxLayout; // 垂直布局容器
pVBLayout_Right->setMargin(10); // 设置四周边距
pVBLayout_Right->addLayout(pHBLayout);
pVBLayout_Right->addWidget(m_pLabel_6);
pVBLayout_Right->addWidget(m_pTE_Description);
m_pPB_OK = new QPushButton(tr("OK"));
m_pPB_Exit = new QPushButton(tr("Exit"));
QHBoxLayout *pHBLayout_Button = new QHBoxLayout; // 水平布局容器
pHBLayout_Button->addStretch(); // 占位符,延伸块
pHBLayout_Button->addWidget(m_pPB_OK);
pHBLayout_Button->addWidget(m_pPB_Exit);
QGridLayout *pGLayout_Main = new QGridLayout(this); // 本窗口布局
pGLayout_Main->setMargin(15);
pGLayout_Main->setSpacing(10);
pGLayout_Main->addLayout(pGLayout_Left, 0, 0);
pGLayout_Main->addLayout(pVBLayout_Right, 0, 1);
pGLayout_Main->addLayout(pHBLayout_Button, 1, 0, 1, 2);
pGLayout_Main->setSizeConstraint(QLayout::SetFixedSize); // 设置布局为固定大小, 无法调整窗口大小
connect(m_pPB_Exit, SIGNAL(clicked()), this, SLOT(close()));
}


资源文件:

icon.png

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: