效果图如下:
QPushButton
各种样式设置:font-family
为设置字体类型,标准形式需要加双引号,不加也可能会生效,具体看系统是否支持,中英文都支持,但要保证字体编码支持,一般程序编码为"utf-8"时没问题。font-size
为设置字体大小,单位一般使用 px 像素font-style
为设置字体斜体,italic 为斜体, normal 为不斜体font-weight
为设置字体加粗,bold 为加粗, normal 为不加粗font
为同时设置字体 style weight size family
的样式,但是 style 和 weight 必须出现在开头,size 和 family 在后面,而且 size 必须在 family 之前,否则样式将不生效,font 中不能设置颜色,可以单独设置 style weight 和 size,不能单独设置 familycolor
为设置字体颜色,可以使用十六进制数表示颜色,也可以使用某些特殊的字体颜色:red, green, blue 等,或者使用 rgb(r,g,b) 和 rgba(r,g,b,a) 来设置,其中 r、g、b、a 值为0~255,如果想不显示颜色可以设置值为透明 transparent注意: 字体颜色用的是 color 属性,没有 font-color 这个属性的
left right top bottom center
;值 left right center 为设置水平位置,值 top bottom center 为设置垂直位置padding-left
为设置文字距离左边边界的距离padding-top
为设置文字距离顶边边界的距离padding-right
为设置文字距离右边边界的距离padding-bottom
为设置文字距离底边边界的距离Tip: 特殊的位置可以使用 text-align
来设置,如果要精确设置位置只能通过 padding 来设置,一般 padding-left
相当于 x 坐标,padding-top
相当于 y 坐标,设置这两个就可以显示任意位置显示了(默认情况下文字是上下左右都居中显示的)
border-style
为设置边框样式,solid 为实线, dashed 为虚线, dotted 为点线, none 为不显示(如果不设置 border-style 的话,默认会设置为 none)border-width
为设置边框宽度,单位为 px 像素border-color
为设置边框颜色,可以使用十六进制数表示颜色,也可以使用某些特殊的字体颜色:red, green, blue 等,或者使用 rgb(r,g,b) 和 rgba(r,g,b,a) 来设置,其中 r、g、b、a 值为0~255,如果想不显示颜色可以设置值为透明 transparentborder
为同时设置 border 的 width style color 属性,但值的顺序必须是按照 width style color 来写,不然不会生效!也可以单独设置某一条边框的样式
border-top-style
为设置顶部边框样式border-top-width
为设置顶部边框宽度border-top-color
为设置顶部边框颜色border-top
为设置顶部边框 width style color
的属性,原理和 border 一致其它三个边框:right bottom left
边框的设置都相同
设置边框半径
border-top-left-radius
为设置左上角圆角半径,单位 px 像素border-top-right-radius
为设置右上角圆角半径,单位 px 像素border-bottom-left-radius
为设置左下角圆角半径,单位 px 像素border-bottom-right-radius
为设置右上角圆角半径,单位 px 像素border-radius
为设置所有边框圆角半径,单位为 px 像素,通过圆角半径可以实现圆形的 PushButtonbackground-color
为设置背景颜色,可以使用十六进制数表示颜色,也可以使用某些特殊的字体颜色:red, green, blue
等,或者使用 rgb(r,g,b)
和 rgba(r,g,b,a)
来设置,其中 r、g、b、a
值为0~255
,如果想不显示颜色可以设置值为透明 transparent
background-image
为设置背景图片,图片路径为 url(image-path)
background-repeat
为设置背景图是否重复填充背景,如果背景图片尺寸小于背景实际大小的话,默认会自动重复填充图片,可以设置为 no-repeat
不重复,repeat-x
在x轴重复,repeat-y
在y轴重复background-position
为设置背景图片显示位置,只支持 left right top bottom center
;值 left right center
为设置水平位置,值 top bottom center
为设置垂直位置background
为设置背景的所有属性,color image repeat position
这些属性值出现的顺序可以任意效果图
QPuahButton
进行动态样式设置鼠标悬浮时的样式
鼠标点击时的样式
按钮禁止时的样式
对于 QPushButton
,可以给它设置添加一个下拉菜单,这需要调用 QPushButton
的 setMenu()
方法,当菜单设置成功后,QPushButton
就会默认添加一个 menu-indicator
下拉菜单指示器图标,我们可以对这个菜单图标进行样式修改
image
为设置菜单指示器图标subcontrol-position
为设置菜单指示器图标的位置,如果不设置的话会默认放在右下角处subcontrol-origin
为设置菜单指示器图标与按钮之间的停靠位置,默认为 paddingright top left bottom
为设置菜单指示器图标距离按钮四个位置的距离对于 menu-indicator
来说 pressed
和 open
原理相同
联系客服