2013-07-07 10 views
15

में QInputDialog से कई इनपुट प्राप्त करना मैं QtCreator में चार इनपुट लेबल से चार मानों का एक सेट प्राप्त करना चाहता हूं। मैं QInputDialog का उपयोग करना चाहता हूं लेकिन इसमें डिफ़ॉल्ट रूप से केवल एक inputbox शामिल है। तो, मैं चार लेबल और चार लाइन-संपादन कैसे जोड़ सकता हूं और इससे मूल्य प्राप्त कर सकता हूं?QtCreator

उत्तर

18

आप नहीं करते हैं। प्रलेखन बहुत स्पष्ट है:

QInputDialog वर्ग एक एकल उपयोगकर्ता से मूल्य प्राप्त करने के लिए एक सरल सुविधा संवाद प्रदान करता है।

यदि आप एकाधिक मान चाहते हैं, तो 4 इनपुट फ़ील्ड के साथ स्क्रैच से QDialog व्युत्पन्न कक्षा बनाएं।

उदाहरण के लिए:

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(); 
    } 
} 
+0

यह संभव था इनपुट के खेतों और संवाद बॉक्स में लेबल के लिए? –

+0

@ गोथम मैंने अपने जवाब में एक उदाहरण जोड़ा। – alexisdm

+0

QDialogButtonBox के उल्लेख के लिए बहुत धन्यवाद, मुझे जो चाहिए था लेकिन नहीं मिला ... –

संबंधित मुद्दे