从Qt中的QInputDialog获取多个输入
Getting multiple inputs from QInputDialog in Qt
我想从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
}
相关文章:
- 如何使用 < 和 > 命令获取 c++ 中的输入和输出?
- 如何从用户那里获取输入并将其存储在向量中?
- 获取通过重定向传入的输入文件的名称?
- Cryptopp:获取密码输入的填充字符串
- 从格式化输入操作中获取读取字符数
- 如何获取用户输入的算术表达式,如 5-8+7*4-8+9 或 1+5?
- 我无法从 C# 获取 PInvoke 的输入参数以C++ DLL 以用作 IntPtr 的输出
- 我需要帮助在一个类中输入两个字符串并获取其输出
- 用于获取枚举值的用户输入
- C++如何使用表达式获取用户输入?
- 从 cin 获取 c++ 中结构中多个枚举的输入
- 代码在执行之前终止,并且没有从用户那里获取输入
- 适用于 macOS 的 Xcode 应用程序。这就是我设置从USB麦克风输入获取音频的方式。一年前工作,现在没有了。为什么
- 推荐一种在未输入获取行分隔符时引发异常的方法?
- 输入获取线的结尾
- 线程不刷新数据,无法从标准输入获取所有数据
- 如何从同一输入获取两个输入 (C++)
- 在C++中静态地将用户输入获取到类中
- 在使用C++字符串时对特定输入获取bad_alloc
- 使用MPI程序,所有进程都从用户输入获取数据