基本布局-QHBoxLayout类、QVBoxLayout类、QGridLayout类

基本布局-QHBoxLayout类、QVBoxLayout类、QGridLayout类(1)新建QtWidgetApplication,项目名UserInfo,基类QDialog,取消创建界面;(2)打开dialog.h头文件,在头文件中声明对话框中的各个控件,添加代码#ifndefDIALOG_H#defineDIALOG_H#include//添加头文件#include#include#inclu

大家好,又见面了,我是你们的朋友全栈君。

(1)新建Qt Widget Application,项目名UserInfo,基类QDialog,取消创建界面;
(2)打开dialog.h头文件,在头文件中声明对话框中的各个控件,添加代码

#ifndef DIALOG_H
#define DIALOG_H

#include <QDialog>
//添加头文件
#include <QLabel>
#include <QLineEdit>
#include <QComboBox>
#include <QTextEdit>
#include <QGridLayout>
class Dialog : public QDialog
{
    Q_OBJECT

public:
    Dialog(QWidget *parent = 0);
    ~Dialog();
private://添加代码如下
    //左侧
    QLabel *UserNameLabel;
    QLabel *NameLabel;
    QLabel *SexLabel;
    QLabel *DepartmentLabel;
    QLabel *AgeLabel;    
    QLabel *OtherLabel;
    QLineEdit *NameLineEdit;
    QLineEdit *UserNameLineEdit;
    QComboBox *SexComboBox;
    QTextEdit *DepartmentTextEdit;
    QLineEdit *AgeLineEdit;
    QGridLayout *LeftLayout;
    //右侧
    QLabel *HeadLabel;//右上角
    QLabel *HeadIconLabel;
    QPushButton *UpdateHeadBtn;
    QHBoxLayout *TopRightLayout;
    QLabel *IntroductionLabel;
    QTextEdit *IntroductionTextEdit;
    QVBoxLayout *RightLayout;
    //底部
    QPushButton *OkBtn;
    QPushButton *CancelBtn;
    QHBoxLayout *ButtomLayout;
};

#endif // DIALOG_H

(2)打开dialog.cpp文件,在类Dialog的构造函数中添加如下代码:

#include "dialog.h"
//添加头文件
#include <QLabel>
#include <QLineEdit>
#include <QComboBox>
#include <QPushButton>
#include <QFrame>
#include <QGridLayout>
#include <QPixmap>
#include <QHBoxLayout>
Dialog::Dialog(QWidget *parent)
    : QDialog(parent)
{
    setWindowTitle(tr("UserInfo"));
    /***********左侧*********/
    UserNameLabel=new QLabel(tr("用户名: "));
    UserNameLineEdit=new QLineEdit;
    NameLabel=new QLabel(tr("姓名:"));
    NameLineEdit=new QLineEdit;
    SexLabel=new QLabel(tr("性别:"));
    SexComboBox=new QComboBox;
    SexComboBox->addItem(tr("女"));
    SexComboBox->addItem(tr("男"));
    DepartmentLabel=new QLabel(tr("部门:"));
    DepartmentTextEdit=new QTextEdit;
    AgeLabel=new QLabel(tr("备注:"));
    AgeLineEdit=new QLineEdit;
    OtherLabel=new QLabel(tr("备注:"));
    OtherLabel->setFrameStyle(QFrame::Panel|QFrame::Sunken);
    //设置控件的风格,setFrameStyle()是QFrame的方法,参数以或|的方式设定控件的面板风格,由形状(QFrame::Shape)和阴影(QFrame::shadow)两项配合决定。其中形状包括六种,分别是NoFrame,Panel,Box,HLine,VLine,WinPanel;阴影包括三种,Plain,Raised,Sunken.
    LeftLayout=new QGridLayout();//左部布局,由于此布局管理器不是主布局管理器,所以不用指定父窗口
    //向布局中加入需要布局的控件
    LeftLayout->addWidget(UserNameLabel,0,0);//用户名
    LeftLayout->addWidget(UserNameLineEdit,0,1);
    LeftLayout->addWidget(NameLabel,1,0);//姓名
    LeftLayout->addWidget(NameLineEdit,1,1);
    LeftLayout->addWidget(SexLabel,2,0);//性别
    LeftLayout->addWidget(SexComboBox,2,1);
    LeftLayout->addWidget(DepartmentLabel,3,0);//部门
    LeftLayout->addWidget(DepartmentTextEdit,3,1);
    LeftLayout->addWidget(AgeLabel,4,0);//年龄
    LeftLayout->addWidget(AgeLineEdit,4,1);
    LeftLayout->addWidget(OtherLabel,5,0,1,2);//其他
    LeftLayout->setColumnStretch(0,1);
    LeftLayout->setColumnStretch(1,3);
    //设定两列分别占用空间的比例,本例设定为1:3,即使对话框大小改变了,两列之间的宽度比依然保存不变
    /**********右侧***********/
    HeadLabel =new QLabel(tr("头像:"));//右上角部分
    HeadIconLabel=new QLabel;
    QPixmap icon("312.pgn");
    HeadIconLabel->setPixmap(icon);
    HeadIconLabel->resize(icon.width(),icon.height());
    UpdateHeadBtn=new QPushButton(tr("更新"));
    //完成右上侧头像选择区的布局
    TopRightLayout=new QHBoxLayout();
    TopRightLayout->setSpacing(20);//设定各个控件之间的间距为20
    TopRightLayout->addWidget(HeadLabel);
    TopRightLayout->addWidget(HeadIconLabel);
    TopRightLayout->addWidget(UpdateHeadBtn);
    IntroductionLabel=new QLabel(tr("个人说明:"));//右下角部分
    IntroductionTextEdit=new QTextEdit;
    //完成右侧的布局
    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);
    //实现主布局,指定父窗口this,也可调用this->setLayout(mainLayout)实现
    mainLayout->setMargin(15);//设定对话框的边距为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);
    //设置最优化显式,即使控件按其sizeHint()的大小显式,并且使用户无法改变对话框大小。
}

Dialog::~Dialog()
{

}

这里写图片描述

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/151802.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • Macbook pro/air 2013 late -2014 使用转接卡更换NVME SSD休眠不醒问题的解决办法

    Macbook pro/air 2013 late -2014 使用转接卡更换NVME SSD休眠不醒问题的解决办法、、1.手上512GMBP2013late差不多满了,因为穷,所以在淘宝上买了一个NVME转Macbookpcie,然后再买一个NVME2T的硬盘2.NVME因为需要最新的FirmwareRom支持,所以必须使用原装的硬盘(必须原装)安装Mac14以上,我安装了14.5.要不然识别不出来新安装的NVME硬盘3.买之前就知道是会有休眠问题的,问了卖家推荐了一些型号说不…

    2022年6月23日
    46
  • pyquery安装

    pyquery安装pyquery是一个类似jquery的工具,不过它是在服务端进行处理的,不像jquery是在浏览器中进行处理。如果我们要进行网络爬虫,爬取有用的信息,那么它是我至今见到的不二选择。我们当然可以自己爬取网页,然后可以通过正则表达式,选取有用的信息,但这其实要求挺高的。我以前也做过爬虫工具,专门抓取招聘网站的招聘信息,但我发先我以前做的实在是复杂。而我们程序员很重要的一点是,不要重复的发明轮子,我们只

    2022年6月6日
    102
  • 武侠金曲「建议收藏」

    十首香港武侠金曲唱出无限中国意境

    2022年4月13日
    40
  • JS-JavaScript学习笔记(一)[通俗易懂]

    JS-JavaScript学习笔记(一)

    2022年1月23日
    49
  • PDMan-国产免费通用数据库建模工具(极简,漂亮)

    背景情况说明  本人长期以来一直从事于金融应用软件的研发以及项目实施工作,经常做数据库建模(数据表设计)。有一款称心如意的数据库建模工具,自然能够事半功倍,PowerDesigner的pdm模型为我的工作提供了很大的便利性。但电脑换了Mac系统之后,就只能在虚拟机Windows上使用PD,机器越来越吃不消了。PD是一款商业化优秀的建模工具。其设计初衷就是用作数据库建模,所以他必然是一款非常优秀的数…

    2022年4月17日
    70
  • WIin10——QTP10.0运行mgn-mqt82未能生成lservrc文件

    WIin10——QTP10.0运行mgn-mqt82未能生成lservrc文件今天在Win10系统安装了QTP10.0,安装步骤都是按照激活成功教程教程执行的:1.安装qtp,一路默认下来,到要求输入License的界面2.拷贝mgn-mqt82.exe(下载)到C:\ProgramFiles\MercuryInteractive(自己手动创建)文件夹下3.自己手动创建C:\ProgramFiles\CommonFiles\MercuryInteractive…

    2022年9月1日
    4

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号