• Qt之布局管理——(1)基本布局管理


    Qt提供的布局类以及他们之间的继承关系(如下图):

    clip_image002

    常用到的布局类有:QHBoxLayout、QVBoxLayout、QGridLayout三种,分别是水平排列布局、垂直排列布局、表格排列布局。

    常用的方法有addWidget()和addLayout()。addWidget()用于在布局中插入控件,addLayout()用于在布局中插入子布局。


    在布局管理中还常用到setMargin()用于设定边距,setSpacing()用于设定控件间距。

    setColumnStretch()用于设置列的占空比。



    示例:实现如下图的布局

    image

    basiclayout.h

    #ifndef BASICLAYOUT_H
    #define BASICLAYOUT_H
    
    #include <QtGui>
    
    
    
    class BasicLayout : public QDialog
    {
    	Q_OBJECT
    
    public:
    	BasicLayout(QWidget *parent = 0, Qt::WFlags flags = 0);
    	~BasicLayout();
    
    private:
    	QLabel *labUser;
    	QLabel *labName;
    	QLabel *labSex;
    	QLabel *labDepartment;
    	QLabel *labAge;
    	QLabel *labRemark;
    	QLineEdit *edtUser;
    	QLineEdit *edtName;
    	QComboBox *cbbSex;
    	QTextEdit *edtDepartment;
    	QLineEdit *edtAge;
    
    	QLabel *labHead;
    	QLabel *labIcon;
    	QLabel *labIndividual;
    	QPushButton *btnChange;
    	QTextEdit *edtIndividual;
    
    	QPushButton *btnOk;
    	QPushButton *btnCancel;
    };
    
    #endif // BASICLAYOUT_H
    

    basiclayout.cpp

    #include "basiclayout.h"
    
    BasicLayout::BasicLayout(QWidget *parent, Qt::WFlags flags)
    	: QDialog(parent, flags)
    {
    	setWindowTitle(tr("User Infomation"));
    	
    	//Left Loyout:
    	labUser = new QLabel(tr("User Name:"));
    	labName = new QLabel(tr("Name;"));
    	labSex = new QLabel(tr("Sex:"));
    	labDepartment = new QLabel(tr("Department:"));
    	labAge = new QLabel(tr("Age:"));
    	labRemark = new QLabel(tr("Remark:"));
    	labRemark->setFrameStyle(QFrame::Panel|QFrame::Sunken);
    	edtUser = new QLineEdit;
    	edtName = new QLineEdit;
    	cbbSex = new QComboBox;
    	cbbSex->insertItem(0,tr("Female"));
    	cbbSex->insertItem(1,tr("Male"));
    	edtDepartment = new QTextEdit;
    	edtAge = new QLineEdit;
    
    	QGridLayout *leftLayou = new QGridLayout;
    	int col_Lab = 0;
    	int col_Content = 1;
    	leftLayou->addWidget(labUser,0,col_Lab);
    	leftLayou->addWidget(edtUser,0,col_Content);
    	leftLayou->addWidget(labName,1,col_Lab);
    	leftLayou->addWidget(edtName,1,col_Content);
    	leftLayou->addWidget(labSex,2,col_Lab);
    	leftLayou->addWidget(cbbSex,2,col_Content);
    	leftLayou->addWidget(labDepartment,3,col_Lab);
    	leftLayou->addWidget(edtDepartment,3,col_Content);
    	leftLayou->addWidget(labAge,4,col_Lab);
    	leftLayou->addWidget(edtAge,4,col_Content);
    	leftLayou->addWidget(labRemark,5,col_Lab,1,2);
    	leftLayou->setColumnStretch(0,1);	//设置两列分别占有空间的比例
    	leftLayou->setColumnStretch(1,3);
    
    	//Right Layout:
    	labHead = new QLabel(tr("Head:"));
    	labIcon = new QLabel;
    	QPixmap m_icon("head.gif");
    	labIcon->resize(m_icon.width(),m_icon.height());
    	labIcon->setPixmap(m_icon);
    	btnChange = new QPushButton(tr("Change"));
    	QHBoxLayout *headLayout = new QHBoxLayout;
    	headLayout->addWidget(labHead);
    	headLayout->addWidget(labIcon);
    	headLayout->addWidget(btnChange);
    	headLayout->setSpacing(20);	//控件间距为20像素
    	labIndividual = new QLabel(tr("Individual:"));
    	edtIndividual = new QTextEdit;
    	QVBoxLayout *rightLayout = new QVBoxLayout;
    	rightLayout->addLayout(headLayout);
    	rightLayout->addWidget(labIndividual);
    	rightLayout->addWidget(edtIndividual);
    	rightLayout->setMargin(10);
    
    	//Bottom Layout:
    	btnOk = new QPushButton(tr("Ok"));
    	btnCancel = new QPushButton(tr("Cancel"));
    	QHBoxLayout *bottomLayout = new QHBoxLayout;
    	bottomLayout->addStretch();	//添加一个占位符
    	bottomLayout->addWidget(btnOk);
    	bottomLayout->addWidget(btnCancel);
    	bottomLayout->setSpacing(10);
    
    	//Main Layout:
    	QGridLayout *mainLayout = new QGridLayout(this);
    	mainLayout->addLayout(leftLayou,0,0);
    	mainLayout->addLayout(rightLayout,0,1);
    	mainLayout->addLayout(bottomLayout,1,0,1,2);
    	mainLayout->setMargin(15);
    	mainLayout->setSpacing(10);
    	mainLayout->setSizeConstraint(QLayout::SetFixedSize);	//设置对话框大小固定,不允许用户改变
    
    }
    
    BasicLayout::~BasicLayout()
    {
    
    }
    

    setFrameStyle()是QFrame的方法,参数以或的方式设定控件的风格,参数1(QFrame::Shape)用于设定控件的形状,参数2(QFrame::Shadow)用于设定控件俺的阴影。

    形状有:NoFrame、Panel、Box、HLine、VLine、WinPanel 6种;阴影有:Plain、Raised、Sunken三种。


  • 相关阅读:
    java语言基础
    常用4种限流算法介绍及比较
    如何用Redis实现分布式锁
    翻转字符串
    JAVA之io流
    JAVA之Collections集合
    【转】IT行业岗位以及发展方向
    JAVA之字符串
    JAVA之数组
    Linux之判断字符串是否为空
  • 原文地址:https://www.cnblogs.com/hanzhaoxin/p/2771861.html
Copyright © 2020-2023  润新知