×
Namespaces

Variants
Actions

How to take input from user using QInputDialog in Qt

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


Contents

Overview

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

Function

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

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

#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

  • When you run this project emulator will shows the button on the screen.


Inoutdialog.JPG

  • When you click on the button following dialog will be shown.


Inoutdialog1.JPG

  • After entering a text in the dialog when OK button is pressed, text will be displayed on the main window as a label.


Inoutdialog2.JPG

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

Was this page helpful?

Your feedback about this content is important. Let us know what you think.

 

Thank you!

We appreciate your feedback.

×