Qt5.9中用QPushbutton按钮做出QCheckBox效果(函数QPushbutton::setCheckable())(当前状态、鼠标滑过状态、选中后状态)

本文主要总结用QPushbutton类做出QCheckBox的效果,同时用QSS实现按钮默认图标和颜色字体,鼠标滑过状态,选中后状态。

1.1下面,将关键代码贴出如下:

QPushButton *allSelectBtn = new QPushButton("全选");
    allSelectBtn->setCheckable(true);
    allSelectBtn->setFixedSize(QSize(95,20));
    allSelectBtn->setStyleSheet("QPushButton{background-image: url(:res/recoverydata/icon/main_icon_choice.png);background-repeat: no-repeat;background-position:left;border:none;color:rgb(204, 204, 204);}"
                                "QPushButton:hover{background-image: url(:res/recoverydata/icon/main_icon_choice_hover.png);background-repeat: no-repeat;background-position:left;border:none;color:rgb(8, 52, 127);}"
                                "QPushButton:checked{background-image: url(:res/recoverydata/icon/main_icon_choice_press.png);background-repeat: no-repeat;background-position:left;border:none;color:rgb(8, 52, 127);}");
    connect(allSelectBtn,SIGNAL(clicked(bool)),this,SLOT(On_allSelectBtnSlot()));


void RecoveryToolFragment::On_allSelectBtnSlot()
{
    if(allSelectBtn->isChecked())
        qDebug()<<"allSelectBtn is checked!";
    else
        qDebug()<<"allSelectBtn is not checked!";
}


1.2实现效果图如下:

默认状态


鼠标滑过状态


          

选中后状态


           

取消选中后状态


1.3其中将QPushbutton变为QCheckBox功能,只需要调用一个函数,QPushbutton::setCheckable(bool)为true就可以了,接下来就可以实现QCheckBox勾选和不勾选效果。



参考内容:

http://doc.qt.io/archives/qt-4.8/qabstractbutton.html

猜你喜欢

转载自blog.csdn.net/naibozhuan3744/article/details/80926532