从QtCreator中的QInputDialog获取多个输入

时间:2013-07-07 13:23:31

标签: c++ qt qt-creator

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

2 个答案:

答案 0 :(得分:22)

你没有。文档很清楚:

  

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

答案 1 :(得分:0)

alexisdm's answer之后,这是一种实现自定义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
}