| 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 |
This code snippet demonstrates how to take input from user using QInputDialog in Qt for S60.
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.
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);
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);
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));
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);
#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);
}
#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
No related wiki articles found