Qt: QPushButton 按钮实现 上图标下文字

 效果如下:

 实现有如下几种方式:

1. 使用 QPushButton 设置 setStyleSheet

例:

    ui->recorder->setStyleSheet("QPushButton{"\
                                "border: 1px solid #00d2ff; "\
                                "min-height: 60px;          "\
                                "color: #FFFFFF;;                 "\
                                "font-size:16px;            "\
                                "text-align : bottom;         "\
                                "background-image: url(:/record.png);"\
                                "background-repeat: repeat-no-repeat;"\
                                "background-position: top;"\
                                "background-origin:content;"\
                                "padding: 10px;"\
                                "background-color: rgb(0, 0, 0);"\
                                "}");

2.QPushButton用qss自定义设置上图标下文字的样式

    QPushButton#pushButton_recorder{
    padding-top: 2px;
    padding-bottom: 2px;
    padding-left: 4px;
    padding-right: 4px;
    border: none;
    font: bold 16px;
    color: #FFFFFF;
    text-align:bottom;
    border-radius: 6px;
    background-color: #000000;
    background-image: url(:/images/recorder.png);
    background-position: top;
    background-origin: content;
    background-repeat: no-repeat;
)

3.使用QToolButton设置Qt::ToolButtonTextUnderIcon

ui->toolButton->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);

qss样式设置:

QToolButton#toolButtonRecorder {
qproperty-icon:url(:/images/recorder.png);
qproperty-iconSize: 40px 40px;
border: 1px solid transparent;
border-radius: 15px;
background-color: transparent;
font-size: 16px;
color:#0B68D0;
border-radius: 6px;
background-color: #FFFFFF;
}

4. 自定义 QPushButton 代码重写  QPushButton类自定义上图标下文字的按钮控件类

void MyPushButton::drawIcon(QPainter *painter)
{
   const QRect btnRect = QRect(10, 10, 80, 80);
   const QSize iconSize = QSize(40, 40);

   QPixmap pix(":/images/close.png");
   painter->save();
   painter->setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform);


   painter->drawPixmap(QRect((btnRect.width()  - iconSize.width() ) / 2,
                             6,
                             iconSize.width(),
                             iconSize.height()),
                             pix);
   painter->restore();
}

void MyPushButton::drawText(QPainter *painter)
{
    const QRect btnRect = QRect(10, 10, 80, 80);
    const QSize iconSize = QSize(40, 40);

    painter->save();
    painter->setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing);

    QFont font = this->font();

    font.setPixelSize(16);

    painter->setFont(font);

    painter->drawText(QRect( 0,
                             (6 + iconSize.height()),
                             btnRect.width(),
                             (btnRect.height() - (6 + iconSize.height()))),
                             Qt::AlignCenter,
                             "关闭");

    painter->restore();
}

void MyPushButton::paintEvent(QPaintEvent *event)
{
    QPainter painter(this);
    QStyleOption opt;
    opt.init(this);
    style()->drawPrimitive(QStyle::PE_Widget,&opt, &painter, this);
    QPushButton::paintEvent(event);
    drawIcon(&painter);
    drawText(&painter);
}

  • 14
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

恋恋西风

up up up

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值