从Qt中的QInputDialog获取多个输入

Getting multiple inputs from QInputDialog in Qt

本文关键字:输入 获取 QInputDialog Qt 中的      更新时间:2023-10-16

我想从Qt中的四个输入标签中获得一组四个值。我想使用QInputDialog,但它只包含一个inputbox作为默认值。那么,我如何添加四个标签的四行编辑并从中获得值呢?

您没有。文档非常清晰:

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

根据alexisdm的回答,这里有一种实现自定义QInputDialog的方法。

"inputdialog.h"

#ifndef INPUTDIALOG_H
#define INPUTDIALOG_H
#include <QDialog>
class QLineEdit;
class QLabel;
class InputDialog : public QDialog
{
    Q_OBJECT
public:
    explicit InputDialog(QWidget *parent = nullptr);
    static QStringList getStrings(QWidget *parent, bool *ok = nullptr);
private:
    QList<QLineEdit*> fields;
};
#endif // INPUTDIALOG_H

"inputdialog.cpp"

#include "inputdialog.h"
#include <QLabel>
#include <QLineEdit>
#include <QDialogButtonBox>
#include <QFormLayout>
InputDialog::InputDialog(QWidget *parent) : QDialog(parent)
{
    QFormLayout *lytMain = new QFormLayout(this);
    for (int i = 0; i < 4; ++i)
    {
        QLabel *tLabel = new QLabel(QString("Text_%1:").arg(i), this);
        QLineEdit *tLine = new QLineEdit(this);
        lytMain->addRow(tLabel, tLine);
        fields << tLine;
    }
    QDialogButtonBox *buttonBox = new QDialogButtonBox
            ( QDialogButtonBox::Ok | QDialogButtonBox::Cancel,
              Qt::Horizontal, this );
    lytMain->addWidget(buttonBox);
    bool conn = connect(buttonBox, &QDialogButtonBox::accepted,
                   this, &InputDialog::accept);
    Q_ASSERT(conn);
    conn = connect(buttonBox, &QDialogButtonBox::rejected,
                   this, &InputDialog::reject);
    Q_ASSERT(conn);
    setLayout(lytMain);
}
QStringList InputDialog::getStrings(QWidget *parent, bool *ok)
{
    InputDialog *dialog = new InputDialog(parent);
    QStringList list;
    const int ret = dialog->exec();
    if (ok)
        *ok = !!ret;
    if (ret) {
        foreach (auto field, dialog->fields) {
            list << field->text();
        }
    }
    dialog->deleteLater();
    return list;
}

现在可以使用类似于QInputDialog::getText():的getStrings()方法

QStringList list = InputDialog::getStrings(this);
if (!list.isEmpty()) {
    // use list
}

bool ok;
QStringList list = InputDialog::getStrings(this, &ok);
if (ok) {
    // use list
}