QMessageBox带有“不再显示此内容”复选框

时间:2016-02-01 12:35:09

标签: c++ qt checkbox qmessagebox

如何在下方显示带有“不再显示”复选框的消息框?

我想象的是这样的事情:

enter image description here

1 个答案:

答案 0 :(得分:12)

Qt 5.2增加了向QCheckBox添加QMessageBox的可能性。看看QMessageBox::setCheckbox

这是一些演示代码

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();
}