首页 > 编程开发 > C类语言    日期:2022-08-07 / 浏览

效果图:

C++ qt实现打开关闭状态按钮的代码

上述这种按钮,用QCheckBox可以实现,只要在选择与未选择的状态设置不同的图片即可:
选择

C++ qt实现打开关闭状态按钮的代码

未选择

C++ qt实现打开关闭状态按钮的代码

实现代码

#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即可。

觉得上面的内容有用吗?快来点个赞吧!

点赞() 我要打赏

温馨提示 : 本站内容来自会员投稿以及互联网,所有源码及教程均为作者总结编辑,请大家在使用过程中提前做好备份,以免发生无法预知的错误,源码类教程请勿直接用于生产环境!

 可能感兴趣的文章