Tôi muốn nhận được một bộ bốn giá trị từ bốn nhãn đầu vào trong QtCreator. Tôi muốn sử dụng QInputDialog
nhưng chỉ chứa một inputbox
làm hình thức mặc định. Vì vậy, làm cách nào tôi có thể thêm bốn nhãn và bốn dòng chỉnh sửa và nhận giá trị từ nó?Nhận nhiều đầu vào từ QInputDialog trong QtCreator
15
A
Trả lời
18
Bạn không. Các tài liệu là khá rõ ràng:
các QInputDialog lớp cung cấp một hộp thoại thuận tiện đơn giản để có được một giá trị đơn từ người dùng.
Nếu bạn muốn có nhiều giá trị, hãy tạo lớp bắt nguồn QDialog
từ đầu với 4 trường nhập.
Ví dụ:
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();
}
}
Là nó có thể có các trường đầu vào và nhãn trong hộp thoại? –
@Gowtham Tôi đã thêm một ví dụ vào câu trả lời của mình. – alexisdm
Rất cám ơn vì đã đề cập đến QDialogButtonBox, những gì tôi cần nhưng không thể tìm thấy ... –