【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);
相关推荐
KaMeidebaby6 分钟前
卡梅德生物技术快报|原核膜蛋白表达优化实操手册,膜蛋白的纯化梯度洗脱完整流程
前端·网络·数据库·人工智能·算法
Summer-Bright17 分钟前
深度 | Agent 协议标准化:一场决定了 AI 经济底层规则的基础设施战争
java·数据库·人工智能·ai
我叫张小白。25 分钟前
LangChain 结构化输出(Structured Output)技术文档
java·数据库·langchain
吴声子夜歌28 分钟前
MongoDB 4.x——数据模型
数据库·mongodb
七夜zippoe1 小时前
DolphinDB实时排名计算:Top-N实时排行
数据库·dolphindb·计算·实时排名·top-n
cmes_love1 小时前
CME、LME、CBOT、NYMEX等交易所外盘期货tick和分钟历史行情数据下载和分析
数据库·区块链
猫猫不是喵喵.1 小时前
双亲委派机制与类加载过程
java·开发语言
大模型码小白1 小时前
向量化引擎与 AI 排障:当 SIMD 遇到异常检测,存储诊断的范式转移
java·大数据·数据库·人工智能·python
程序员清风1 小时前
公司要裁员之前,都会有哪些信号?
java·后端·面试
Flittly1 小时前
【AgentScope Java新手村系列】(20)文字转语音TTS
java·spring boot·spring