How to take input from user using QInputDialog in Qt
Overview[edit | edit source]
This code snippet demonstrates how to take input from user using QInputDialog in Qt.
Function[edit | edit source]
Basically QInputDialog allows four different types of the input that can be taken from the user. These input types are string, integer,Double or a item selected from a list.
- QInputDialog::getText ( this, const QString & title, const QString & label, QLineEdit::EchoMode mode = QLineEdit::Normal, const QString & text = QString(), bool * ok = 0)
In the above function first argument specify the parent of that dialog,next is title that can be displayed as a dialog title, third is the text that is shown to the user it must tell the user what is to be enter, next is the mode used by the lineedit, Text is the default text that can be shown in the LineEdit and last one is set to 1 if user click ok and set to 0 if user click on the cancel.
QString text = QInputDialog::getText(this, tr("Enter Name"),tr("User name:"), QLineEdit::Normal,"", &ok);
- QString QInputDialog::getItem ( this, const QString & title, const QString & label, const QStringList & list, int current = 0, bool editable = true, bool * ok = 0)
In this function first three argument are the same as the previous one. Forth argument is the string list that is to be display in the dialog and current indicate which item is the current item.
QStringList city;
city << tr("Bhavnagar") << tr("Ahmedabad") << tr("Mumbai") << tr("Delhi");
bool ok;
QString item = QInputDialog::getItem(this, tr("getItem()"),tr("City"), city, 0, false, &ok);
if (ok && !item.isEmpty())
itemLabel->setText(item);
- int QInputDialog::getInteger ( this, const QString & title, const QString & label, int value = 0, int minValue = -100, int maxValue = 100, int step = 2, bool * ok = 0 )
While taking integer as a user input it basically generates a spinbox and user has to select a value from that spinbox using a arrow. Here in this function first three and last argument are the same as previous two function. Value indicate the default value of the spinbox. Minvalue and maxvalue indicate the range of the spinbox value, Step indicate the step change in the value when user click on the arrow.
int i = QInputDialog::getInteger(this, tr("getInteger()"),tr("Value"), 25, 0, 100, 1, &ok);
if (ok)
label->setText(tr("%1%").arg(i));
- double QInputDialog::getDouble ( this, const QString & title, const QString & label, double value = 0, double minValue = -100, double maxValue = 100, int decimals = 1, bool * ok = 0)
This function can be used to get a floating point number from the user. It is very similar to getInteger() function.
double d = QInputDialog::getDouble(this, tr("getDouble()"),
tr("Float number"), 37.56, -10000, 10000, 2, &ok);
- More Details about QInputDialog
Source File[edit | edit source]
- include "dialoginput.h"
dialoginput::dialoginput(QWidget *parent)
: QDialog(parent)
{
button = new QPushButton("Click Me",this);
connect(button, SIGNAL(clicked()), this, SLOT(setText()));
label =new QLabel(this);
layout = new QVBoxLayout(this);
layout->addWidget(button);
layout->addWidget(label);
setLayout(layout);
}
dialoginput::~dialoginput()
{
//parent object will destroy its child.
}
void dialoginput::setText()
{
bool ok;
QString text = QInputDialog::getText(this, tr("Enter Name"),
tr("User name:"), QLineEdit::Normal,
"", &ok);
if (ok && !text.isEmpty())
label->setText(text);
}
Header File[edit | edit source]
- ifndef DIALOGINPUT_H
- define DIALOGINPUT_H
- include <QtGui/QDialog>
- include "ui_dialoginput.h"
- include <QPushButton>
- include <QInputDialog>
- include <QLabel>
- include <QVBoxLayout>
class dialoginput : public QDialog
{
Q_OBJECT
public:
dialoginput(QWidget *parent = 0);
~dialoginput();
private slots:
void setText();
private:
QPushButton *button;
QLabel *label;
QVBoxLayout *layout;
};
- endif // DIALOGINPUT_H
Screenshots[edit | edit source]
- When you run this project emulator will shows the button on the screen.
- When you click on the button following dialog will be shown.
- After entering a text in the dialog when OK button is pressed, text will be displayed on the main window as a label.