QPushButton使用css设置图标和文本

编程知识 更新时间:2023-04-06 07:30:07

平时我们设置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设置图标和文本

本文发布于:2023-04-06 07:30:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/00a7ce91b8ed20edb92dc97e393b054f.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:图标   文本   QPushButton   css

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!

  • 48672文章数
  • 14阅读数
  • 0评论数