在 QMessageBox 中设置多个复选框

Setting multiple checkboxes in QMessageBox

我想在一个QMessageBox 中设置两个QCheckBox。仅出现第二个复选框。我如何实现这一目标?只创建一个自定义 QDialog 更好吗?

    void TextEditor::actionConfigure_triggered()
    {
        QCheckBox *checkbox = new QCheckBox("Cursor to end of file");
        QCheckBox *geometryCheckBox = new QCheckBox("Save and restore geometry");
        QMessageBox msgBox(this);
        msgBox.setStandardButtons(QMessageBox::Apply | QMessageBox::Discard | QMessageBox::Reset | QMessageBox::RestoreDefaults);
        msgBox.setDefaultButton(QMessageBox::Apply);
        msgBox.setCheckBox(checkbox);
        msgBox.setCheckBox(geometryCheckBox);
        checkbox->setToolTip("Option to move cursor to end of text on file open");
        geometryCheckBox->setToolTip("Option to save and restore geometry on open / close");
        int ret = msgBox.exec();
        switch (ret) {
          case QMessageBox::Apply:
              // Save was clicked
              break;
          case QMessageBox::Discard:
              // Don't Save was clicked
              break;
          case QMessageBox::Reset:
            // Cancel was clicked
            break;
          case QMessageBox::RestoreDefaults:
            // Restore defaults
            break;
          default:
            // should never be reached
            break;
        }
    }

QMessageBox 默认只允许放置一个 QCheckBox,因此如果添加了一个新的 QCheckBox,它将替换之前的 QCheckBox。一种可能的解决方案是直接将 QCheckBox 注入到布局中:

QCheckBox *checkbox = new QCheckBox("Cursor to end of file");
QCheckBox *geometryCheckBox = new QCheckBox("Save and restore geometry");
QMessageBox msgBox(this);
msgBox.setStandardButtons(QMessageBox::Apply | QMessageBox::Discard | QMessageBox::Reset | QMessageBox::RestoreDefaults);
msgBox.setDefaultButton(QMessageBox::Apply);
checkbox->setToolTip("Option to move cursor to end of text on file open");
geometryCheckBox->setToolTip("Option to save and restore geometry on open / close");
msgBox.setCheckBox(checkbox);

QGridLayout *grid = qobject_cast<QGridLayout *>(msgBox.layout());
int index = grid->indexOf(checkbox);
int row, column, rowSpan, columnSpan;
grid->getItemPosition(index, &row, &column, &rowSpan, &columnSpan);
grid->addWidget(geometryCheckBox, row + 1,  column, rowSpan, columnSpan);

int ret = msgBox.exec();