QInputDialog Multiple Inputs 輸入多個變量的對話框


 

在之前的博客QInputDialog 使用方法中展示了利用QInputDialog可以快速通過一行代碼來生成一個輸入框,來獲取用戶的輸入值,那么如果我們希望獲取多個輸入值,怎么辦呢?那么此時用QInputDialog就沒法實現了,我們必須基於QDialog類重新寫一個類,可是只是一個簡單的多值輸入框,我們又不想為了它而生成對應的.cpp和.h,還有.ui文件,這樣太麻煩了,其實我們可以用代碼來添加輸入框和對應的label。

 

 

如果我們想生成一個上圖一樣的多個輸入值的文本對話框,可以使用如下代碼:

 

QDialog dialog(this);
QFormLayout form(&dialog);
form.addRow(new QLabel("User input:"));
// Value1
QString value1 = QString("Value1: ");
QSpinBox *spinbox1 = new QSpinBox(&dialog);
form.addRow(value1, spinbox1);
// Value2
QString value2 = QString("Value2: ");
QSpinBox *spinbox2 = new QSpinBox(&dialog);
form.addRow(value2, spinbox2);
// Add Cancel and OK button
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()));

// Process when OK button is clicked
if (dialog.exec() == QDialog::Accepted) {
    // Do something here
}

 

參考資料:

http://stackoverflow.com/questions/17512542/getting-multiple-inputs-from-qinputdialog-in-qtcreator

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM