Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

QMessageBox with a "Do not show this again" checkbox

How can I show a message box with a "Do not show again" checkbox below?

I imagine something that looks like this:

enter image description here

like image 566
user5820174 Avatar asked Feb 01 '16 12:02

user5820174


1 Answers

Qt 5.2 added the possibility to add a QCheckBox to a QMessageBox. Have a look at QMessageBox::setCheckbox

Here is some demo code

if (this->showMsgBox) {
    QCheckBox *cb = new QCheckBox("Okay I understand");
    QMessageBox msgbox;
    msgbox.setText("Am I nerve-wrecking?");
    msgbox.setIcon(QMessageBox::Icon::Question);
    msgbox.addButton(QMessageBox::Ok);
    msgbox.addButton(QMessageBox::Cancel);
    msgbox.setDefaultButton(QMessageBox::Cancel);
    msgbox.setCheckBox(cb);

    QObject::connect(cb, &QCheckBox::stateChanged, [this](int state){
        if (static_cast<Qt::CheckState>(state) == Qt::CheckState::Checked) {
            this->showMsgBox = false;
        }
    });

    msgbox.exec();
}
like image 158
Christian Rapp Avatar answered Nov 13 '22 19:11

Christian Rapp