You Are Here:

Community: Wiki

This page was last modified on 30 October 2009, at 16:04.

How to take input from user using QInputDialog in Qt for Symbian

From Forum Nokia Wiki



ID ... Creation date 26 January 2009
Platform S60 3rd Edition FP1, S60 3rd Edition FP2, S60 5th Edition Tested on devices Emulator
Category Qt for Symbian Subcategory


Keywords (APIs, classes, methods, functions): QPushButton,QInputDialog,QLabel


Overview

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

Preconditions


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.


Image:Inoutdialog.JPG

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


Image: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.


Image:Inoutdialog2.JPG

Rate This

 
Bookmark this page: DeliciousDiggFacebookGoogleYahooStumbleUponRedditDiigoTechnocratiTwitter  Share this page Share this page Print this Page Print this page Invite a friend Invite a friend
京ICP备05048969号    Email Newsletters Press Terms & Conditions Privacy Policy Sitemap Contact Us © 2009 Nokia 
RDF Facets: qdcZidentifierQSxhttpE3aE2fE2fwikiE2eforumE2enokiaE2ecomE2findeE78E2ephpE2fTalkE3aE4cargeE5fscreenE5fsaverX qdcZtypeQUqfnZE45E78cludedFromGeneralE4cistingsQ qdcZtypeQUqfntypeZCommunityContentQ qdcZtypeQUqfntypeZE52esourceQ qdcZtypeQUqfntypeZWebpageQ qdcZtypeQUqfntypeZWikiContentQ qdcZtypeQUqmarsZManagedE52esourceQ qdcZtypeQUqwebZInformationE52esourceQ qdcZtypeQUqwebZPageQ qdcZtypeQUqwebZE52esourceQ qdcZtypeQUqrdfsZE52esourceQ qfnZtypeQUqfntypeZCommunityContentQ qfnZtypeQUqfntypeZE52esourceQ qfnZtypeQUqfntypeZWebpageQ qfnZtypeQUqfntypeZWikiContentQ qmarsZlanguageQUxhttpE3aE2fE2fswE2enokiaE2ecomE2flanguageE2d1E2fenX qrdfZtypeQUqfnZE45E78cludedFromGeneralE4cistingsQ qrdfZtypeQUqfntypeZCommunityContentQ qrdfZtypeQUqfntypeZE52esourceQ qrdfZtypeQUqfntypeZWebpageQ qrdfZtypeQUqfntypeZWikiContentQ qrdfZtypeQUqmarsZManagedE52esourceQ qrdfZtypeQUqwebZInformationE52esourceQ qrdfZtypeQUqwebZPageQ qrdfZtypeQUqwebZE52esourceQ qrdfZtypeQUqrdfsZE52esourceQ