9

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

4

1 回答 1

19

Qt 5.2 增加了将 a 添加QCheckBox到 a的可能性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();
}
于 2016-02-01T14:02:28.233 回答