PDA

View Full Version : taking input from the input box



neutrino
30th September 2011, 11:52
i want to take input from a textbox/textarea.
what are the classes needed for it?
and if i want to check the validity of the input(like int/float etc.) are there any inbuilt functions?
i am a newbie in Qt programming.

Ethan
30th September 2011, 12:04
Hello neutrino,

If you mean with textarea a QLineEdit class you can use the the text() method. Take a look here (http://doc.qt.nokia.com/4.8-snapshot/qlineedit.html#text-prop)

With regards checking the input I would suggest to take a look to the methods of Qstring class reference, here (http://doc.qt.nokia.com/4.8-snapshot/qstring.html); and QByteArray, here (http://doc.qt.nokia.com/4.8-snapshot/qbytearray.html).

-E

neutrino
30th September 2011, 12:12
can you give an example code.
this is my first project(better to say first code)in Qt.

deepakn
30th September 2011, 12:12
Take a look at QInputDialog (http://doc.qt.nokia.com/latest/qinputdialog.html) if you want to receive values directly from user. For validating, inputdialog itself has different functions using which you can differentiate between double, int etc.

Ethan
30th September 2011, 13:06
Let me know if you dont understand any line of the code. I used qdebug(), then you can see when pressing a Qpushbutton the string data is captured and can be seen with Qt Creator (in application output).

-E


#include <QtGui>

class ExampleForNeutrino: public QWidget
{
Q_OBJECT
public:

ExampleForNeutrino(QWidget *p = 0): QWidget(p)
{
textUser = new QLineEdit(tr("Insert text"));
captureText = new QPushButton(tr("CaptureText"));
connect(captureText,SIGNAL(clicked()),this, SLOT(storeText()));

QVBoxLayout *layout = new QVBoxLayout();
layout->addWidget(textUser);
layout->addWidget(captureText);
setLayout(layout);

}

public slots:
void storeText()
{
QByteArray str = textUser->text().toAscii();
qDebug() << "Text captured by neutrino: " << str;
}

private:
QLineEdit *textUser;
QPushButton *captureText;
};

int main(int argc, char *argv[])
{

QApplication a(argc, argv);

ExampleForNeutrino e;
e.show();
return a.exec();
}
#include "main.moc"

neutrino
30th September 2011, 13:25
cannot find main.moc

Let me know if you dont understand any line of the code. I used qdebug(), then you can see when pressing a Qpushbutton the string data is captured and can be seen with Qt Creator (in application output).

-E


#include <QtGui>

class ExampleForNeutrino: public QWidget
{
Q_OBJECT
public:

ExampleForNeutrino(QWidget *p = 0): QWidget(p)
{
textUser = new QLineEdit(tr("Insert text"));
captureText = new QPushButton(tr("CaptureText"));
connect(captureText,SIGNAL(clicked()),this, SLOT(storeText()));

QVBoxLayout *layout = new QVBoxLayout();
layout->addWidget(textUser);
layout->addWidget(captureText);
setLayout(layout);

}

public slots:
void storeText()
{
QByteArray str = textUser->text().toAscii();
qDebug() << "Text captured by neutrino: " << str;
}

private:
QLineEdit *textUser;
QPushButton *captureText;
};

int main(int argc, char *argv[])
{

QApplication a(argc, argv);

ExampleForNeutrino e;
e.show();
return a.exec();
}
#include "main.moc"

Ethan
30th September 2011, 16:59
In my case that code works (i did it and compiled in my pc)
Clean the .pro file and leave only the line "SOURCES += main.cpp", and rebuild. Let me know if it works.

If not, take a look here (http://www.qtcentre.org/threads/36006-Unable-to-compile-an-Example?)

-E

neutrino
30th September 2011, 17:09
my file name was different.
i changed it to main.cpp and it worked.
thanks