日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學(xué)無先后,達(dá)者為師

網(wǎng)站首頁 編程語言 正文

C++?qt實現(xiàn)打開關(guān)閉狀態(tài)按鈕的代碼_C 語言

作者:令狐掌門 ? 更新時間: 2022-05-18 編程語言

效果圖:

在這里插入圖片描述

上述這種按鈕,用QCheckBox可以實現(xiàn),只要在選擇與未選擇的狀態(tài)設(shè)置不同的圖片即可:
選擇

在這里插入圖片描述

未選擇

在這里插入圖片描述

實現(xiàn)代碼

#include "widget.h"
#include "ui_widget.h"
#include 
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里設(shè)置QCheckBox::indicator:unchecked與QCheckBox::indicator:checked兩種轉(zhuǎn)態(tài)下不同的背景圖,當(dāng)選擇狀態(tài)發(fā)生變化時,鏈接信號stateChanged即可。

原文鏈接:https://blog.csdn.net/yao_hou/article/details/123488871

欄目分類
最近更新