15
我想從QtCreator中的四個輸入標籤獲得一組四個值。我想使用QInputDialog
,但它只包含一個inputbox
作爲默認值。那麼,如何添加四個標籤和四行編輯並從中獲得價值?在QtCreator中從QInputDialog獲取多個輸入
我想從QtCreator中的四個輸入標籤獲得一組四個值。我想使用QInputDialog
,但它只包含一個inputbox
作爲默認值。那麼,如何添加四個標籤和四行編輯並從中獲得價值?在QtCreator中從QInputDialog獲取多個輸入
你沒有。該文檔是相當清楚的:
的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();
}
}
是它可以在對話框中輸入字段和標籤? –
@Gowtham我給我的答案增加了一個例子。 – alexisdm
非常感謝提及QDialogButtonBox,我需要但找不到... –