Qt通过QSS设置QPushButton的样式

同时设置QPushButton的文字样式和图标的方法

  1. 为了美化界面,有时候需要修改QPushButton的样式,让一个QPushButton上面既要显示图标,又要显示文字内容

    • 起初我的做法是重写QPushButton,这样做可以实现,但是有几个问题
      • 实现比较繁琐
      • 每次使用UI编辑器设计界面的时候,对每一个QPushButton都要做一次提升
    • 为了解决上面的问题,就使用QSS的方式实现
  2. QPushButton上面既显示文字又显示图标的需要

    • 通过qss样式便可以在一个QPushButton上面,左边显示图标,右边显示文字

    • 效果图
      在这里插入图片描述

    • 具体代码实现如下:

QPushButton#btnUpdate{
	font: 9pt ".萍方-简";
	font-style: normal;
	font-size: 12px;
	line-height: 20px;
	border-radius: 4px;
	background: rgb(0, 142, 250);
	color: #FFFFFF;
	text-align: right;
    padding-right: 16px;
	background-image: url(:/UpdateLauncher/Source/Check.png);
    background-origin: content;
    background-position: left;
    padding-left: 16px;
    background-repeat: no-repeat;
}
QPushButton#btnUpdate:hover{
	font: 9pt ".萍方-简";
	font-style: normal;
	font-size: 12px;
	line-height: 20px;
	border-radius: 4px;
	background: rgb(0, 100, 250);
	color: #FFFFFF;
	text-align: right;
    padding-right: 16px;
	background-image: url(:/UpdateLauncher/Source/Check.png);
    background-origin: content;
    background-position: left;
    padding-left: 16px;
    background-repeat: no-repeat;
}
QPushButton#btnUpdate:pressed{
	font: 9pt ".萍方-简";
	font-style: normal;
	font-size: 12px;
	line-height: 20px;
	border-radius: 4px;
	background: rgb(0, 142, 250);
	color: #FFFFFF;
	text-align: right;
    padding-right: 16px;
	background-image: url(:/UpdateLauncher/Source/Check.png);
    background-origin: content;
    background-position: left;
    padding-left: 16px;
    background-repeat: no-repeat;
}

你可能感兴趣的:(qt,开发语言)