我想从 Qt 中的四个输入标签中获取一组四个值。我想使用 QInputDialog
但它只包含一个 inputbox
作为默认值。那么,如何添加四个标签和四个行编辑并从中获取值(value)?
最佳答案
你没有。文档很清楚:
The QInputDialog class provides a simple convenience dialog to get a single value from the user.
如果您需要多个值,请从头开始创建一个 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();
}
}
关于c++ - 从 Qt 中的 QInputDialog 获取多个输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17512542/