从QtCreator中的QInputDialog获取多个输入

Gow*_*nan 14 c++ qt qt-creator

我想从QtCreator中的四个输入标签中获取一组四个值.我想使用,QInputDialog但它只包含一个inputbox作为默认值.那么,我如何添加四个标签四个行编辑并从中获取价值?

ale*_*sdm 22

你没有.文档很清楚:

QInputDialog类提供了一个简单的便捷对话框,用于从用户获取 单个值.

如果需要多个值,请QDialog从头开始创建一个带有4个输入字段的派生类.

例如:

QDialog dialog(this);
// Use a layout allowing to have a label next to each field
QFormLayout form(&dialog);

// Add some text above the fields
form.addRow(new QLabel("The question ?"));

// Add the lineEdits with their respective labels
QList<QLineEdit *> fields;
for(int i = 0; i < 4; ++i) {
    QLineEdit *lineEdit = new QLineEdit(&dialog);
    QString label = QString("Value %1").arg(i + 1);
    form.addRow(label, lineEdit);

    fields << lineEdit;
}

// Add some standard buttons (Cancel/Ok) at the bottom of the dialog
QDialogButtonBox buttonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel,
                           Qt::Horizontal, &dialog);
form.addRow(&buttonBox);
QObject::connect(&buttonBox, SIGNAL(accepted()), &dialog, SLOT(accept()));
QObject::connect(&buttonBox, SIGNAL(rejected()), &dialog, SLOT(reject()));

// Show the dialog as modal
if (dialog.exec() == QDialog::Accepted) {
    // If the user didn't dismiss the dialog, do something with the fields
    foreach(QLineEdit * lineEdit, fields) {
        qDebug() << lineEdit->text();
    }
}
Run Code Online (Sandbox Code Playgroud)