本文简单的用到了网格布局,水平布局,竖直布局的一些方法,模仿了QQ的个人信息修改窗口,先是对左半步分个人信息进行网格布局,然后对右半步分的头像和个人说明进行布局,最后是底层水平布局确定和取消,这里用到了Qt占位符的知识。最后将这三部分布局用网格布局组成顶级布局,底层布局占用了一行两列。
头文件:
#ifndef DIALOG_H #define DIALOG_H #include <QtGui/QDialog> #include <QLabel> #include <QLineEdit> #include <QTextEdit> #include <QComboBox> #include <QGridLayout> #include <QPushButton> class Dialog : public QDialog { Q_OBJECT public: Dialog(QWidget *parent = 0); ~Dialog(); QLabel *label1; QLabel *label2; QLabel *label3; QLabel *label4; QLabel *label5; QLabel *label6; QLabel *label7; QLabel *labelOther; QLabel *labelIcon; QLineEdit *lineEditUser; QLineEdit *lineEditName; QComboBox *comboBoxSex; QTextEdit *textEditDepartment; QLineEdit *lineEditAge; QTextEdit *textEditDisc; QPushButton *pushButtonIcon; QPushButton *pushButtonOK; QPushButton *pushButtonExit; }; #endif // DIALOG_H |
#include "dialog.h" Dialog::Dialog(QWidget *parent) : QDialog(parent) { setWindowTitle(tr("User Name")); label1 = new QLabel(tr("User Name")); label2 = new QLabel(tr("Name")); label3 = new QLabel(tr("Sex")); label4 = new QLabel(tr("Department")); label5 = new QLabel(tr("Age")); labelOther = new QLabel(tr("Remark")); //label继承自QFrame,可以设置凹陷或阴影状态,用|即可 labelOther->setFrameStyle(QFrame::Panel | QFrame::Sunken); lineEditUser = new QLineEdit; lineEditName = new QLineEdit; //QComBox的使用,insertItem插入项 comboBoxSex = new QComboBox; comboBoxSex->insertItem(0,tr("Female")); comboBoxSex->insertItem(1,tr("Male")); textEditDepartment = new QTextEdit(); lineEditAge = new QLineEdit; //左半边子布局网格布局 QGridLayout *leftLayout = new QGridLayout(); //只有两列,所以定义两个变量labelCol表示第0列,contentCol表示第一列 int labelCol=0; int contentCol=1; leftLayout->addWidget(label1,0,labelCol); leftLayout->addWidget(lineEditUser,0,contentCol); leftLayout->addWidget(label2,1,labelCol); leftLayout->addWidget(lineEditName,1,contentCol); leftLayout->addWidget(label3,2,labelCol); leftLayout->addWidget(comboBoxSex,2,contentCol); //注意这里的部门标签,设置为顶部对齐方式,可以和textEdit对齐,看运行效果 leftLayout->addWidget(label4,3,labelCol,Qt::AlignTop); leftLayout->addWidget(textEditDepartment,3,contentCol); leftLayout->addWidget(label5,4,labelCol); leftLayout->addWidget(lineEditAge,4,contentCol); //labelOther坐标(5,0),占了一行布局中两列的位置,看运行效果 leftLayout->addWidget(labelOther,5,labelCol,1,2); //setColumnStretch设置第0列在布局管理中所占比例为1,第1列在布局管理中所占比例为3, //即使对话框大小改变了,两列之间的宽度比依然保持不变 leftLayout->setColumnStretch(0,1); leftLayout->setColumnStretch(1,3); label7 = new QLabel(tr("Head")); labelIcon = new QLabel(); QPixmap icon("man.png"); labelIcon->resize(icon.width(),icon.height()); labelIcon->setPixmap(icon); pushButtonIcon = new QPushButton; pushButtonIcon->setText(tr("Change")); QHBoxLayout *hlayout = new QHBoxLayout; hlayout->setSpacing(20); hlayout->addWidget(label7); hlayout->addWidget(labelIcon); hlayout->addWidget(pushButtonIcon); label6 = new QLabel(tr("Individual")); textEditDisc = new QTextEdit; //右半部分的竖直对齐 QVBoxLayout *rightLayout = new QVBoxLayout; //设置控件到边框的距离 rightLayout->setMargin(10); rightLayout->addLayout(hlayout); rightLayout->addWidget(label6); rightLayout->addWidget(textEditDisc); //底布局,两个按钮 pushButtonOK = new QPushButton(tr("OK")); pushButtonExit = new QPushButton(tr("Cancel")); QHBoxLayout *bottomLayout = new QHBoxLayout; //addStretch在按钮之前插入一个占位符,使两个按钮能始终靠右对齐, //并且在整个对话框的大小发生改变时,保证按钮的大小不发生变化 bottomLayout->addStretch(); bottomLayout->addWidget(pushButtonOK); bottomLayout->addWidget(pushButtonExit); //顶级布局,布局管理的对象会默认被布局管理所在控件管理,即默认指定父窗口 QGridLayout *mainLayout = new QGridLayout(this); mainLayout->setMargin(15); mainLayout->setSpacing(10); mainLayout->addLayout(leftLayout,0,0); mainLayout->addLayout(rightLayout,0,1); mainLayout->addLayout(bottomLayout,1,0,1,2); //setSizeConstraint(Qlayout::setFixedSize)设定对话框的控件总是最优化显示, //并且用户无法改变对话框的大小,所谓最优化显示,即控件都按其sizeHint() //的大小显示,sizeHint表示布局管理器中默认尺寸 mainLayout->setSizeConstraint(QLayout::SetFixedSize); } Dialog::~Dialog() { } |