当前位置: 首页 > news >正文

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);
}

http://www.lryc.cn/news/386177.html

相关文章:

  • 使用阿里云效API操作流水线
  • 使用命令行创建uniapp+TS项目,使用vscode编辑器
  • ABC355 Bingo2
  • Spring+Vue项目部署
  • 【uml期末复习】统一建模语言大纲
  • Linux高级IO
  • go-admin-ui开源后台管理系统华为云部署
  • 点云入门知识
  • HTML静态网页成品作业(HTML+CSS+JS)——家乡莆田介绍网页(5个页面)
  • #### grpc比http性能高的原因 ####
  • 微软Edge浏览器搜索引擎切换全攻略
  • <Linux> 实现命名管道多进程任务派发
  • BigInteger 和 BigDecimal(java)
  • Linux 进程间通讯
  • 数据分析三剑客-Matplotlib
  • FastAPI-Body、Field
  • 软件设计师笔记-操作系统知识(二)
  • 鸿蒙UI开发快速入门 —— part12: 渲染控制
  • 添加用户页面(Flask+前端+MySQL整合)
  • 素数筛(算法篇)
  • 迁移Microsoft Edge
  • Maven高级理解属性
  • Trilium Notes浏览器插件保存网页内容到docker私有化部署
  • C++ 统计二进制串中0出现的个数
  • note-网络是怎样连接的6 请求到达服务器,响应返回浏览器
  • 存储过程与函数:封装数据库逻辑的艺术(七)
  • 【复旦邱锡鹏教授《神经网络与深度学习公开课》笔记】卷积
  • Trie字符串统计
  • Kali Linux源
  • 【RT摩拳擦掌】基于RT106L/S语音识别的百度云控制系统