QT学习 第二章:布局管理--基本布局管理
2011-05-30 15:35
561 查看
QT学习 第二章:布局管理--基本布局管理
效果截图:
创建文件夹LayoutDlg,编写三个源码文件:
资源文件:
icon.png
效果截图:
创建文件夹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
相关文章推荐
- Qt之布局管理——(1)基本布局管理
- Qt 实现基本布局管理 ---实验
- Qt学习08——布局管理综合应用
- QT布局管理程序实现的学习
- Qt布局管理基本实例
- Qt学习07——布局管理
- Qt之布局管理——(1)基本布局管理
- QT布局管理程序实现的学习及 QTextEdit编辑
- Qt 第6章 布局管理(2) 分组布局 学习笔记
- Qt 第6章 布局管理(3) 切分布局管理 学习笔记
- Qt 第6章 布局管理(1) 在窗体中摆放窗口部件 学习笔记
- Qt学习:布局管理的使用方法
- Qt 第6章 布局管理(4) 滚动区域 学习笔记
- Qt学习笔记(三)布局管理
- Qt5开发学习之布局管理(三)
- PyQt学习笔记01-Qt基本概念进阶篇之窗口布局,对话框
- Qt之布局管理——(1)基本布局管理
- SQLserver学习day03 数据表基本概念和操作的学习与管理 数据库关系图
- C++ GUI QT 第4版 第六章 布局管理 (3)
- 【Linux学习笔记】Linux_01_基本命令、文件管理、正则