目录
1.按钮头文件
#include <QPushButton> //调用按钮头文件
2.QPushButton常用用法
方法 | 功能 |
setFixedSize(w,h) | 设置控件窗口大小 |
setMinimumSize(w,h) | 在使用setFixedSize后仍然可用来设置窗体的最小值 |
setMaximumSize(w,h) | 在使用setFixedSize后仍然可用来设置窗体的最大值 |
setGeometry(x,y,l,w) | 在屏幕上(x,y)的地方显示一个l*w的界面,但受layout布局限制,只能在父控件的范围内调整位置,在使用了此函数后一定要调用show函数,否则可能看不到控件 |
setDefault(bool) | 设置默认选中按钮(回车时有效) |
3.setStyleSheet详解
3.1.border
pushbutton->setStyleSheet("QPushButton{border:1px solid red;...}"); //基本样式
pushbutton->setStyleSheet("QPushButton{border:%1...}").arg(border); //外部调用时
a.border设置的是四个边框的宽度、样式和颜色
border:border-width border-style border-color;
border-width
1.可以是thin,medium,thick
2.也可以用长度单位定值,可以用绝对长度单位(cm, mm, in, pt, pc)或者相对长度单位 (em, ex, px)。
border-style
none: 无样式;
hidden: 同样是无样式,主要用于解决和表格的边框冲突;
dotted: 点划线;
dashed: 虚线;
solid: 实线;
double: 双线,两条线加上中间的空白等于border-width的取值;
groove: 槽状;
ridge: 脊状,和groove相反;
inset: 凹陷;
outset:凸出,和inset相反;
b.单边边框的设置方法
border-top:border-width border-style border-color; //三种属性一起设置
//三种属性分开设置
{
border-top-width:1px;
border-top-style:solid;
border-top-color:#FF0000;
}//也可以
{ border-top:1px solid #FF0000; }
border-right //右边框属性
border-bottom //下边框属性
border-left //左边框属性
c.border-style设置元素所有边框的样式,或者单独地为各边设置边框样式
border-style:solid solid solid solid; //上 右 下 左
当是none时隐藏边框
3.2.border-image
pushbutton->setStyleSheet("QPushButton{border-image:url(:/路径);...}"); //基本样式
pushbutton->setStyleSheet("QPushButton{border-image:url(%1)...}").arg(border-image); //外部调用时
使用了border-image之后,即使设置了border也没有任何边框作用。
注:使用border-image:url(%1)和border:%1的格式,没有url时使用括号会不显示设置后的样式
3.3.background-image
pushbutton->setStyleSheet("QPushButton{background-image:url(:/路径);...}"); //基本样式
pushbutton->setStyleSheet("QPushButton{background-image:url(%1)...}").arg(border-image); //外部调用时
background-repeat:no-repeat //不重复
background-position:center //在中心位置显示
3.4.font
pushbutton->setStyleSheet("QPushButton{font:14px bold "微软雅黑";...}"); //基本样式
pushbutton->setStyleSheet("QPushButton{font:%1...}").arg(font); //外部调用时
3.5.color
参考网址:
- 渐变色的配色方案-科技风:https://uigradients.com/#Joomla
- 渐变色的配色方案:Fresh Background Gradients | WebGradients.com 💎
- 随机三个颜色,喜不喜欢:https://www.palettable.io/EBDFB7-5F4C45-F9F011
- 个性化的配色网站:Colors - House of van Schneider
4.setStyleSheet总结
样式-英文 | 参数 | 样式-中文 |
color | white/rgb(110,110,100)/#eb7350 | 字体颜色 |
background | transparent | 背景为透明 |
background-color | white/rgb(110,110,110)/#eb7350 | 背景颜色 |
background-position | left/right/center/top/bottom | 设定图片的位置 |
background-image:url() | ./img/back.png | 背景图片,不会缩放图片大小 |
background-repeat | no-repeat | 不重复填充图片 |
border-image:url() | ./img/back.png | 背景图片,会对图片进行拉伸,平铺 |
border-style | outset/inset | 边框样式,按下是inset |
border-width | px | 边框大小 |
border-radius | px | 边框弧度 |
border | 3px solid red | 边框宽度及颜色 |
border-color | rgba(255,255,255,0) | 边框颜色,0时透明 |
font-family | 微软雅黑 | 设定字体 |
font | bold 14px | 字体大小并加粗 |
font-size | px | 字体大小 |
font-style | inset | 字体样式 |
font-weight | px | 字体深浅 |
selection-color | color | 设定选中时候的颜色 |