Please note that as of October 24, 2014, the Nokia Developer Wiki will no longer be accepting user contributions, including new entries, edits and comments, as we begin transitioning to our new home, in the Windows Phone Development Wiki. We plan to move over the majority of the existing entries. Thanks for all your past and future contributions.

How to take input from user using QInputDialog in Qt

From Wiki
Jump to: navigation, search
Article Metadata
Tested with
Devices(s): Symbian emulator
Platform(s): Qt
Keywords: QPushButton,QInputDialog,QLabel
Created: james1980 (26 Jan 2009)
Last edited: hamishwillee (11 Oct 2012)



This code snippet demonstrates how to take input from user using QInputDialog in Qt.


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

Source File

#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);
//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())

Header File

#include <QtGui/QDialog>
#include "ui_dialoginput.h"
#include <QPushButton>
#include <QInputDialog>
#include <QLabel>
#include <QVBoxLayout>
class dialoginput : public QDialog
dialoginput(QWidget *parent = 0);
private slots:
void setText();
QPushButton *button;
QLabel *label;
QVBoxLayout *layout;


  • 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.


This page was last modified on 11 October 2012, at 01:17.
1299 page views in the last 30 days.