QT之简单界面布局设计

widget.h:


#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>
#include <QPushButton>
#include <QLineEdit>
#include <QLabel>
#include <QTextEdit>
#include <QComboBox>
#include <QGridLayout>
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QFrame>
#include <QPixmap>
class Widget : public QWidget
{
    Q_OBJECT

public:
    Widget(QWidget *parent = 0);
    ~Widget();
private:
    //左侧
    QLabel *usernamelabel,*namelabel,*sexlabel,*departmentlabel,*agelabel,*otherlabel;
    QLineEdit *usernamelinedit,*namelinedit;
    QComboBox *sexcombobox;
    QTextEdit *departmenttextedit;
    QLineEdit *agelineedit;
    QGridLayout *leftlayout;
    QHBoxLayout *lefthlayout;

    //右侧
    QLabel * headlabel,*updatelabel,*introductionlabel;
    QLabel *headiconlabel;
    QTextEdit *introductiontextedit;
    QHBoxLayout *toprightlayout;
    QVBoxLayout *rightlayout;
    //底部
    QPushButton *okbtn,*cancelbtn;
    QHBoxLayout *buttomlayout;

};

#endif // WIDGET_H


main.cpp:


#include "widget.h"
#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    Widget w;
    w.show();

    return a.exec();
}


widget.cpp:

#include "widget.h"

Widget::Widget(QWidget *parent)
    : QWidget(parent)
{
    setWindowTitle("用户信息");
    //左侧
    usernamelabel = new QLabel(tr("用户名:"));
    namelabel = new QLabel(tr("姓名:"));
    sexlabel = new QLabel(tr("性别:"));
    departmentlabel = new QLabel(tr("部门:"));
    agelabel = new QLabel(tr("年龄:"));
    otherlabel = new QLabel(tr("备注:"));
    usernamelinedit = new QLineEdit;
    namelinedit = new QLineEdit;
    sexcombobox = new QComboBox;
    sexcombobox->addItem(tr("男"));
    sexcombobox->addItem(tr("女"));
    departmenttextedit = new QTextEdit;
    agelineedit = new QLineEdit;
    //QFrame类是有框架的窗口部件的基类
    otherlabel->setFrameStyle(QFrame::Panel|QFrame::Sunken);//在面板中凹陷显示Sunken(凹陷),Raised(上调)
    leftlayout = new QGridLayout();
    leftlayout->addWidget(usernamelabel,0,0);   //用户名
    leftlayout->addWidget(namelabel,1,0);   //姓名
    leftlayout->addWidget(sexlabel,2,0);    //性别
    leftlayout->addWidget(departmentlabel,3,0); //部门
    leftlayout->addWidget(agelabel,4,0);    //年龄
    leftlayout->addWidget(otherlabel,5,0,1,2);  //备注
    leftlayout->addWidget(usernamelinedit,0,1);
    leftlayout->addWidget(namelinedit,1,1);
    leftlayout->addWidget(sexcombobox,2,1);
    leftlayout->addWidget(departmenttextedit,3,1);
    leftlayout->addWidget(agelineedit,4,1);

    leftlayout->setColumnStretch(0,1);  //设置gridlayout每列的占位比,即第一列所占比为1/4
    leftlayout->setColumnStretch(1,3);//第二列所占比为3/4

    //右侧
    headlabel = new QLabel(tr("头像:"));
    headiconlabel = new QLabel;
    QPixmap icon("1.png");
    headiconlabel->setPixmap(icon); //label显示图像
    headiconlabel->resize(icon.width(),icon.height());  //设置label大小与图像大小一致
    updatelabel = new QLabel(tr("更新"));
    introductionlabel = new QLabel(tr("个人说明:"));
    introductiontextedit = new QTextEdit();

    toprightlayout = new QHBoxLayout();
    toprightlayout->setMargin(20);  //设置边框
    toprightlayout->addWidget(headlabel);
    toprightlayout->addWidget(headiconlabel);
    toprightlayout->addWidget(updatelabel);

    rightlayout = new QVBoxLayout();
    rightlayout->setMargin(10); //设置边距
    rightlayout->addLayout(toprightlayout);
    rightlayout->addWidget(introductionlabel);
    rightlayout->addWidget(introductiontextedit);

    //底部
    okbtn = new QPushButton(tr("确定"));
    cancelbtn = new QPushButton(tr("取消"));

    buttomlayout = new QHBoxLayout();
    buttomlayout->addStretch(); //加入一个占位符
    buttomlayout->addWidget(okbtn);
    buttomlayout->addWidget(cancelbtn);

    //整体布局
    QGridLayout *mainlayout = new QGridLayout(this);
    mainlayout->setMargin(15); //表示控件与窗体的左右边距
    mainlayout->setSpacing(10); //表示各个控件之间的上下间距
    mainlayout->addLayout(leftlayout,0,0);
    mainlayout->addLayout(rightlayout,0,1);
    mainlayout->addLayout(buttomlayout,1,0,1,2);
    mainlayout->setSizeConstraint(QLayout::SetFixedSize);   //设置layout大小与控件尺寸一致

}

Widget::~Widget()
{

}

设计界面如下:












评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值