当前位置:网站首页>基本布局-QHBoxLayout类、QVBoxLayout类、QGridLayout类
基本布局-QHBoxLayout类、QVBoxLayout类、QGridLayout类
2022-06-24 21:56:00 【全栈程序员站长】
大家好,又见面了,我是你们的朋友全栈君。
(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.cn/151802.html原文链接:https://javaforall.cn
边栏推荐
- Elastase instructions in Chinese and English
- 创新药二级市场审饼疲劳:三期临床成功、产品获批也不管用了
- Install mysql5.6 under linux64bit - the root password cannot be modified
- 明日考试 最后一天如何备考?二造考点攻略全整理
- 多模态数据也能进行MAE?伯克利&谷歌提出M3AE,在图像和文本数据上进行MAE!最优掩蔽率可达75%,显著高于BERT的15%
- AssertionError: CUDA unavailable, invalid device 0 requested
- 字符串常用方法
- 放养但没有完全放养(春季每日一题 2)
- 通达信哪个开户更安全,更好点
- "One good programmer is worth five ordinary programmers!"
猜你喜欢

论文翻译 | RandLA-Net: Efficient Semantic Segmentation of Large-Scale Point Clouds

Q1季度逆势增长的华为笔电,正引领PC进入“智慧办公”时代

JS array object to object

Abnova丨BSG 单克隆抗体中英文说明

Abnova 5-methylcytosine polyclonal antibody

JVM directive

js数组对象转对象

Multi modal data can also be Mae? Berkeley & Google proposed m3ae to conduct Mae on image and text data! The optimal masking rate can reach 75%, significantly higher than 15% of Bert

1. package your own scaffold 2 Create code module

Dataease template market officially released
随机推荐
C language boundary calculation and asymmetric boundary
Some Modest Advice for Graduate Students - by Stephen C. Stearns, Ph.D.
Day 04 - file IO
Abnova丨BSG 单克隆抗体中英文说明
php中preg_replace如何替换变量数据
通达信哪个开户更安全,更好点
Status quo analysis: how "one cloud and multi-core" can promote the rapid deployment of information innovation projects
屡获大奖的界面控件开发包DevExpress v22.1官宣发布
js数组对象转对象
Tianshu night reading notes -- disassembly engine xde32
PHP easywechat and applet realize long-term subscription message push
广发期货安全吗?开户需要什么东西?
Constant current circuit composed of 2 NPN triodes
Transformers 库的基本使用
菊花链(寒假每日一题 39)
创新药二级市场审饼疲劳:三期临床成功、产品获批也不管用了
多模态数据也能进行MAE?伯克利&谷歌提出M3AE,在图像和文本数据上进行MAE!最优掩蔽率可达75%,显著高于BERT的15%
MySQL multi condition matching fuzzy query
国内炒股开户正规安全的具体名单
JS array object to object