效果图:
上述这种按钮,用QCheckBox可以实现,只要在选择与未选择的状态设置不同的图片即可:
选择
未选择
实现代码
#include "widget.h" #include "ui_widget.h" #include <QMessageBox> Widget::Widget(QWidget *parent) : QWidget(parent) , ui(new Ui::Widget) { ui->setupUi(this); ui->checkBox->setText(""); ui->checkBox->setFixedSize(128, 64); QString qss = "QCheckBox::indicator:unchecked{ \ image:url(:/resources/status_close.png); \ } \ QCheckBox::indicator:checked { \ image: url(:/resources/status_open.png); \ }"; ui->checkBox->setStyleSheet(qss); ui->checkBox->setChecked(true); connect(ui->checkBox, &QCheckBox::stateChanged, this, &Widget::slot_stateChanged); } Widget::~Widget() { delete ui; } void Widget::slot_stateChanged(int state) { if(ui->checkBox->isChecked()) { //QMessageBox::information(this, "tips", "open"); } else { //QMessageBox::information(this, "tips", "close"); } }
在qss里设置QCheckBox::indicator:unchecked与QCheckBox::indicator:checked两种转态下不同的背景图,当选择状态发生变化时,链接信号stateChanged即可。
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。
评论(0)