【Qt】按钮样式--按钮内部布局(调整按钮文本和图标放置在任意位置)

要求:

有一个按钮,要求按钮的右下角显示开关,点击切换开关状态

ps:注意,要求你添加完了之后,整个按钮的点击区域不变(就是说,点击右下角的文本,也可以触发按钮的点击事件,点击按钮中任何地方,都会触发点击事件,就是你在按钮上添加东西,不要影响按钮的点击区域)

效果:

实现:

将你要添加的东西,不管是label还是别的,都放在布局里,然后将按钮的布局设置一下就行。

我这个案例是添加了两个label

c++ 复制代码
    // 设置测试按钮
    testButton = new QPushButton("", this);
    testButton->setMinimumSize(QSize(420, 232));
    testButton->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
    testButton->setProperty("TestButton",true);
    // 设置文字
    textLabel = new QLabel;
    textLabel->setProperty("TestButtonText", "start");
    textLabel->setText("running");
    textLabel->setFixedHeight(32);
    // 设置icon
    iconLabel = new QLabel;
    iconLabel->setFixedSize(32, 32);
    iconLabel->setScaledContents(true);// 强制图片填满label
    QPixmap _backgroundPixmap(":/img/start.png");
    iconLabel->setPixmap(_backgroundPixmap);
    // icon和文字布局
    QHBoxLayout* iconAndTextLayout = new QHBoxLayout();
    iconAndTextLayout->addStretch();
    iconAndTextLayout->addWidget(iconLabel);
    iconAndTextLayout->addWidget(textLabel);
    iconAndTextLayout->setSpacing(10);
    // 按钮布局
    QVBoxLayout* buttonLayout = new QVBoxLayout();
    buttonLayout->addStretch();
    buttonLayout->addLayout(iconAndTextLayout);
    testButton->setLayout(buttonLayout);
相关推荐
知守观9 分钟前
OBS 同名文件互相覆盖,客户拿错了别人的报告:一次文件串号的排查与重构
java·后端
katasea21 分钟前
第05章:信创技术栈选型:服务器、操作系统、数据库、中间件适配对比
服务器·数据库·中间件
梓沂30 分钟前
记一次 Oracle 测试库自动刷新脚本的踩坑实录
数据库·oracle
泡海椒44 分钟前
JQuick-Excel 字段映射实战:用 MAPPING 固化 Excel 表头与业务字段契约
xml·java·开发语言·excel
2501_931803751 小时前
MySQL 索引核心原理
数据库·mysql
浪潮IT馆1 小时前
Windows 10 安装 PostgreSQL 9.6.24 完整教程
数据库·windows·postgresql
梦幻通灵1 小时前
IDEA 实用快捷键【持续更新】
java·ide·intellij-idea
南归北隐1 小时前
Spring AI Alibaba Graph框架实现Tools工具调用
java·后端·spring·spring ai·spring ai tools
开开心心就好1 小时前
视频模糊怎么修复?免费工具支持批量处理
java·前端·人工智能·智能手机·pdf·excel
Quz1 小时前
QML SplitView:可拖拽分隔面板与自定义 Handle
qt