c++ - 从 Qt 中的 QInputDialog 获取多个输入

标签 c++ qt

我想从 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/

相关文章:

c++ - Qt样式表的任意检查

Qt 从相对 URL 创建组件

c++ 写入文件当前行的开头

c++ - QBoxLayout 与 QMainWindow 对比 QWidget

c++ - 在condition_variable::wait()调用过程中中断程序(SIGINT),随后对exit()的调用导致程序卡住

c++ - 如何从 vector<int> 中删除重复项的所有实例

c++ - 如何将图像转换为缓冲区,以便我可以使用套接字编程将其发送过来? C++

c++ - 更改命令行 Qt5 源构建的配置的正确/快速方法

android - 将外部库添加到 Qt-Android 项目中

c++ - 编译我的 C++ 代码需要很长时间