QT布局
一、QHBoxLayout/QVBoxLayout
1.layout通过使用setcontentmargins来设置四周留白的间隔
函数原型:void QLayout::setContentsMargins ( int left, int top, int right, int bottom )
layout1->setContentsMargins(1,0,1,0);
2.layout可以使用setMargin()在控件内部的边距设置;
3.layout可以使用addStretch()在布局里面添加弹簧;
**4.**layout可以使用 setspacing设置控件之间的间隔;注:设置无效的可能原因有,总布局里添加控件的时候设置了比例,如图所示,去掉那个比例就可以了.
5.setStretchFactor设置布局里某控件之间的比例;
mainLayout->setStretchFactor(m_listWidget,1);
mainLayout->setStretchFactor(m_stackWidget,9);
二、QFont(大小/颜色)
1.设置字体的大小:
代码如下(示例):
QFont ft;
ft.setPointSize(25);//字体大小
m_titleLabel->setFont(ft);//其他控件也一样
2.设置字体颜色:
a.样式:
m_titleLabel = new QLabel(tr("客户端"),this);
m_titleLabel->setStyleSheet("color:yellow;");
b.调色板:
QLabel *lable = new QLabel("系统已就绪 ", this);
QPalette pe;
pe.setColor(QPalette::WindowText, Qt::white);//设置颜色
lable ->setPalette(pe);
三、QLinearGradient (颜色渐变—应用背景渐变)
代码如下(示例):
frame->setStyleSheet("background-color:qlineargradient(spread:pad,x1:0, y1:0, x2:1, y2:0,stop:0 #030303,stop:0.5 #030303 ,stop:1 #2E8B57)");
a. spread:pad:渐变的模式为 pad,另外的两个模式为 repeat、reflect。
b. x1、y1、x2、y2 四个坐标表示渐变的坐标范围。
c. x1、y1 对应为左上角坐标,x2、y2 对应右下角坐标,其后面的数字是 0 和 1,表示渐变的顺序。后面的 stop 后面的数值(0 到 1 之间)就是按照数字的大小顺序确定方向来改变颜色。
说明: stop:0 #030303 就是在坐标为 0 的点设置颜色为 #030303 也就是黑色;
stop:0.5 #030303 就是在坐标为 0.5 的位置改变颜色。
上面代码的效果:
四、QGroupBox分组框
前言:分组框通常带有一个边框和一个标题栏,作为容器部件来使用,在其中可以布置各种窗口部件。通常带有一个边框和一个标题栏,作为容器部件来使用,在其中可以布置各种窗口部件。布局时可用作一组控件的容器,但是需要注意的是,内部必须使用布局控件(如QBoxLayout)进行布局。
代码如下(示例):
//创建复选框
QGroupBox *groupBox = new QGroupBox(tr("服务器配置"));
groupBox->setFlat(false);//边框是否有线
groupBox->setCheckable(true);//是否有复选框
groupBox->setChecked(false);//默认是否勾选复选框
QHBoxLayout *mlayout = new QHBoxLayout();
m_ipLabel = new QLabel(tr("服务器ip:"));
m_ipLabel->setFixedSize(120,30);
m_ipLineEdit = new QLineEdit(this);
m_ipLineEdit->setFixedSize(180,40);
m_portLabel = new QLabel(tr("服务器端口:"));
mlayout->addWidget(m_ipLabel);
mlayout->addWidget(m_ipLineEdit);
mlayout->addStretch();
groupBox->setLayout(mlayout); //往里面添加布局
1.setFlat(bool)是否扁平化
组框通常由顶部带有标题的周围框架组成。如果启用此属性,则大多数样式只绘制框架的顶部;否则,将绘制整个框架。
五、QSS(结合setStyleSheet使用)
以QGroupBox为例:
qss文件
QGroupBox
{
border: 2px solid red;
border-radius:5px;
margin-top:2ex;//文字在方框中位置的偏离度---以上边缘为基线
font-family:楷体;
font:bold 28px; //字体大小
}
QGroupBox::title
{
subcontrol-origin:margin;
subcontrol-position:top left;
left:7px;//标题偏离左边缘的距离
margin-left: 0px;
padding:0.1px;
}
代码setStyleSheet的方式设置
代码如下(示例):
groupBox->setStyleSheet("QGroupBox {border-width:1px;border-style:solid;border-color:lightGray;margin-top:1.5ex;}"\
"QGroupBox::title{subcontrol-origin:margin;subcontrol-position:top left;left:7px;margin-left: 0px;padding:0.1px;}");
基本样式设置含义:
font-family:楷体; 为设置字体类型,标准形式需要加双引号,不加也可能会生效,具体看系统是否支持,中英文都支持,但要保证字体编码支持,一般程序编码为"utf-8"时没问题。
font-style:normal;为设置字体斜体样式,italic 为斜体, normal 为不斜体,oblique(倾斜).
font-weight:bold;为设置字体加粗样式,bold 为加粗, normal 为不加粗;
**font-size: 14px;**设置字体大小.
注:字体同时设置时的顺序 style weight size family .
**color: #BDC8E2;**设置字体颜色,可以使用十六进制数表示颜色,也可以使用某些特殊的字体颜色:red, green, blue 等,或者使用 rgb(r,g,b) 和 rgba(r,g,b,a) 来设置,其中 r、g、b、a 值为0~255.
**padding-left: 10px;**设置文字距离左边边界的距离.
**padding-top: 8px;**设置文字距离顶边边界的距离.
**padding-right: 7px;**设置文字距离右边边界的距离.
**padding-bottom: 9px;**为设置文字距离底边边界的距离.
border-style 为设置边框样式,solid 为实线, dashed 为虚线, dotted 为点线, none 为不显示(如果不设置 border-style 的话,默认会设置为 none).
border-width: 2px 为设置边框宽度,单位为 px 像素.
border-color 为设置边框颜色.
注:同时设置的顺序width style color.
**background-color: #2E3648;**设置背景颜色.
**background-image: url(“./image.png”);**设置背景图片,图片路径为 url(image-path).
background-repeat: no-repeat; 为设置背景图是否重复填充背景.
background-position: left center;设置背景图片显示位置,只支持 left right top bottom center;
可参考:
CSS font-family字体大合集