【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);
相关推荐
双力臂40419 分钟前
Spring Boot 单元测试进阶:JUnit5 + Mock测试与切片测试实战及覆盖率报告生成
java·spring boot·后端·单元测试
♡喜欢做梦34 分钟前
【MySQL】深入浅出事务:保证数据一致性的核心武器
数据库·mysql
遇见你的雩风37 分钟前
MySQL的认识与基本操作
数据库·mysql
Edingbrugh.南空37 分钟前
Aerospike与Redis深度对比:从架构到性能的全方位解析
java·开发语言·spring
dblens 数据库管理和开发工具40 分钟前
MySQL新增字段DDL:锁表全解析、避坑指南与实战案例
数据库·mysql·dblens·dblens mysql·数据库连接管理
weixin_4196583141 分钟前
MySQL的基础操作
数据库·mysql
QQ_4376643141 小时前
C++11 右值引用 Lambda 表达式
java·开发语言·c++
永卿0011 小时前
设计模式-迭代器模式
java·设计模式·迭代器模式
誰能久伴不乏1 小时前
Linux如何执行系统调用及高效执行系统调用:深入浅出的解析
java·服务器·前端
慕y2742 小时前
Java学习第七十二部分——Zookeeper
java·学习·java-zookeeper