【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);
相关推荐
nanxun8862 小时前
记一次诡异的 Docker 容器"串包"故障排查
java
xcyxiner3 小时前
DicomViewer (后台线程处理文件)4
qt
用户1563068103515 小时前
Day01 | Java 基础(Java SE)
java
行者全栈架构师6 小时前
Maven dependency:tree 的 8 个高级用法
java·后端
xcyxiner10 小时前
DicomViewer (添加模型类)3
qt
行者全栈架构师10 小时前
IDEA 中 Maven 项目的 15 个红色报错快速解决方法
java·后端
令人头秃的代码0_010 小时前
mac(m5)平台编译openjdk
java
xcyxiner1 天前
DicomViewer (目录调整) 2
qt
xcyxiner1 天前
dcmtk vtk vtk-dicom(gdcm) 编译(debug) v2
qt
倔强的石头_1 天前
《Kingbase护城河》——数据库存储空间全景探测与精细化瘦身实战
数据库