平时我们设置QPushButton时只设置它的normal, hover, pressed三种状态的文本类型或者背景图片,很少用到图标和文本同时显示,本篇介绍图标和文本同时显示,并且图标可以动态调整显示位置,下面是具体的代码:
void MainWindow::initView()
{
ui->pushButton->setStyleSheet("QPushButton{background-color: #66ffff;border-radius:5px;"
"background-image: url(:/arrow_refresh16.png); "
"background-origin: content;background-position: center;"
"padding-right: 40px; padding-bottom: 2px; background-repeat: no-repeat;"
"text-align:top;padding-left: 2px;padding-top: 2px;"
"font-size: 12px; color: #FFFFFF;}");
ui->pushButton_2->setStyleSheet("QPushButton{background-color: #66ffff;border-radius:5px;"
"background-image: url(:/arrow_refresh16.png); "
"background-origin: content;background-position: center;"
"padding-right: 40px; padding-bottom: 2px; background-repeat:repeat;"
"text-align:top;padding-left: 2px;padding-top: 2px;"
"font-size: 12px; color: #FFFFFF;}");
ui->pushButton_3->setStyleSheet("QPushButton{background-color: #66ffff;border-radius:5px;"
"background-image: url(:/arrow_refresh16.png);"
"background-origin: content; background-position: top;"
"background-repeat: no-repeat;"
"text-align: bottom; padding-bottom:5px;"
"font-size: 12px; color: #FF0000;}");
ui->pushButton_4->setStyleSheet("QPushButton{background-color: #66ffff;border-radius:5px;"
"background-image: url(:/arrow_refresh16.png);"
"background-origin: content; background-position: bottom;"
"background-repeat: no-repeat;"
"text-align: top; padding-top:5px;"
"font-size: 12px; color: #FF0000;}");
ui->pushButton_5->setStyleSheet("QPushButton{background-color: #66ffff;border-radius:5px;"
"background-image: url(:/arrow_refresh16.png);"
"background-origin: content; background-position: left;"
"background-repeat: no-repeat;"
"text-align: right; padding-right:5px;"
"font-size: 12px; color: #FF0000;}");
ui->pushButton_6->setStyleSheet("QPushButton{background-color: #66ffff;border-radius:5px;"
"background-image: url(:/arrow_refresh16.png);"
"background-origin: content; background-position: right;"
"background-repeat: no-repeat;"
"text-align: left; padding-top:5px;"
"font-size: 12px; color: #FF0000;}");
}
运行效果:
css的参数参考:
CSS padding 属性
CSS text-align 属性
CSS3 background-origin 属性 | 菜鸟教程
更多推荐
QPushButton使用css设置图标和文本
发布评论