PDA

View Full Version : QLineEdit && QLabel



eleanor
4th November 2006, 11:50
Hey...

So far I've created QLabel + a couple of QLineEdit's and now I'm curious: How can I get the user-input ?

Example: Let's say there are 2 QLineEdit's and user enters 14 in the first one and 16 in the second one. I wish to get both numbers and display 14+16=30 on the screen by QLabel. How can this be done?

jacek
4th November 2006, 11:59
QLineEdit emits signals when user edits it and you can connect to one of them, for example QLineEdit::textChanged() or QLineEdit::editingFinished().

You will know how to do it, if you go through this tutorial: http://doc.trolltech.com/4.1/tutorial-t1.html

eleanor
4th November 2006, 13:55
Look: here's the code:



#include <qapplication.h>
#include <qwidget.h>
#include <qlineedit.h>
#include <qlabel.h>


class mojClass : public QWidget
{
public:
mojClass();
private:
QLineEdit *lineedit1;
QLabel *label1;
QLabel *label2;
QLabel *label3;
};

mojClass::mojClass()
{
setGeometry(100,100,300,200);

lineedit1 = new QLineEdit(this);
lineedit1->setGeometry(110,10,50,20);
lineedit1->setEchoMode(QLineEdit::Normal);
lineedit1->setMaxLength(6);

label1 = new QLabel(this);
label1->setGeometry(10,10,100,20);
label1->setText("Enter something: ");

label2 = new QLabel(this);
label2->setGeometry(10,40,100,20);
label2->setText("You've entered: ");

label3 = new QLabel(this);
label3->setGeometry(120,40,100,20);

connect(lineedit1,SIGNAL(textChanged()),label3,SLO T(setText()));
}

int main(int argc, char **argv)
{
QApplication a(argc,argv);
mojClass objekt;
a.setMainWidget(&objekt);
objekt.show();
a.exec();
}



And here's the error:



QObject::connect: No such signal QLineEdit::textChanged()
QObject::connect: (sender name: 'unnamed')
QObject::connect: (receiver name: 'unnamed')


What am I missing?

jacek
4th November 2006, 15:24
You have to add parameter types, like this:
connect( lineedit1, SIGNAL( textChanged( const QString& ) ), label3, SLOT( setText( const QString& ) ) );

eleanor
4th November 2006, 15:29
Thank you very much, it works:)

I have some other problems. How can I add QLineEdit(this); into a loop. So there would be 1 QLineEdit displayed where the userwould put in a number 25 (just an example) and then 25 more QLineEdit's would be displayed?

jacek
4th November 2006, 15:52
I have some other problems. How can I add QLineEdit(this); into a loop. So there would be 1 QLineEdit displayed where the userwould put in a number 25 (just an example) and then 25 more QLineEdit's would be displayed?
Read this: http://doc.trolltech.com/4.1/tutorial-t6.html

You probably want to use QVBoxLayout instead of QGridLayout (and don't forget to invoke show() on each widget, if you create them after their parent was shown). You can use QList to store pointers to line edits, so that you can delete them when the user wants to reduce their number.

eleanor
5th November 2006, 17:18
How can I accept the number from QLineEdit and then multiply it by 4 and then output it with QLabel?

jpn
5th November 2006, 17:34
How can I accept the number from QLineEdit and then multiply it by 4 and then output it with QLabel?
To restrict the user to input only numbers, you can use a QIntValidator:


QValidator *validator = new QIntValidator(100, 999, this);
QLineEdit *edit = new QLineEdit(this);
// the edit lineedit will only accept integers between 100 and 999
edit->setValidator(validator);

or you can use input mask:


QLineEdit *edit = new QLineEdit(this);
// the edit lineedit will only accept 0-3 digit chars
edit->setInputMask("000");


For multiplying, you need a custom slot:


// class declaration
class mojClass : public ...
{
...
private slots:
void customSlot(const QString& input);
...
};

// class implementation
mojClass::mojClass()
{
...
connect(lineedit1,SIGNAL(textChanged(const QString&)),this,SLO T(customSlot(const QString&)));
}

// the custom slot
void mojClass::customSlot(const QString& input)
{
int num = 4 * input.toInt();
label3->setNum(num);
}

eleanor
5th November 2006, 23:04
This does not work:



#include <qapplication.h>
#include <qwidget.h>
#include <qlineedit.h>
#include <qlabel.h>
#include <qlayout.h>
#include <qvalidator.h>


class mojClass : public QWidget
{
Q_OBJECT
public:
mojClass();
private:
QLineEdit *lineedit1;
QLabel *label1;
QLabel *label2;
QLabel *label3;
QValidator *validator;
private slots:
void customSlot(const QString &input);

};

void mojClass::customSlot(const QString &input)
{
int num = 4 * input.toInt();
label3->setNum(num);
}


mojClass::mojClass()
{
setGeometry(100,100,300,200);

validator = new QIntValidator(100,99999,this);
lineedit1 = new QLineEdit(this);
lineedit1->setGeometry(110,10,50,20);
lineedit1->setEchoMode(QLineEdit::Normal);
lineedit1->setMaxLength(6);
lineedit1->setValidator(validator);

label1 = new QLabel(this);
label1->setGeometry(10,10,100,20);
label1->setText("Enter something: ");

label2 = new QLabel(this);
label2->setGeometry(10,40,100,20);
label2->setText("You've entered: ");

label3 = new QLabel(this);
label3->setGeometry(120,40,100,20);

connect(lineedit1,SIGNAL(textChanged(const QString &)),this,SLOT(customSlot(const QString &)));
}

int main(int argc, char **argv)
{
QApplication a(argc,argv);
mojClass objekt;
a.setMainWidget(&objekt);
objekt.show();
a.exec();
}

jacek
5th November 2006, 23:23
To make it work you have to add Q_OBJECT macro like this:

class mojClass : public QWidget
{
Q_OBJECT
public:
...
Each class that defines new slots or signals must contain Q_OBJECT macro. Also remember to rerun qmake if you add that macro somewhere.

If you have placed everything in a single file, say somefile.cpp, you have to add #include "somefile.moc" at the end and rerun qmake. This isn't necessary if you have placed class definition in a header file.

eleanor
5th November 2006, 23:48
Aha thank you.

If I want to use 3 files, is this correct:

poskus.h


#idndef POSKUS_H
#define POSKUS_H

#include <qwidget.h>

class mojClass : public QWidget
{
Q_OBJECT
public:
mojClass();
private:
QLineEdit *lineedit1;
QLabel *label1;
QLabel *label2;
QLabel *label3;
QValidator *validator;
private slots:
void customSlot(const QString &input);

};

#endif



poskus.cpp


#include "poskus.h"
#include <qlineedit.h>
#include <qlabel.h>
#include <qlayout.h>
#include <qvalidator.h>

void mojClass::customSlot(const QString &input)
{
int num = 4 * input.toInt();
label3->setNum(num);
}


mojClass::mojClass()
{
setGeometry(100,100,300,200);

validator = new QIntValidator(100,99999,this);
lineedit1 = new QLineEdit(this);
lineedit1->setGeometry(110,10,50,20);
lineedit1->setEchoMode(QLineEdit::Normal);
lineedit1->setMaxLength(6);
lineedit1->setValidator(validator);

label1 = new QLabel(this);
label1->setGeometry(10,10,100,20);
label1->setText("Enter something: ");

label2 = new QLabel(this);
label2->setGeometry(10,40,100,20);
label2->setText("You've entered: ");

label3 = new QLabel(this);
label3->setGeometry(120,40,100,20);

connect(lineedit1,SIGNAL(textChanged(const QString &)),this,SLOT(customSlot(const QString &)));
}




main.cpp


#include <qapplication.h>

#include "poskus.h"

int main(int argc, char **argv)
{
QApplication a(argc,argv);
mojClass objekt;
a.setMainWidget(&objekt);
objekt.show();
a.exec();
}

jacek
6th November 2006, 00:02
If I want to use 3 files, is this correct:
It looks OK.

eleanor
7th November 2006, 20:10
Hey.

How can I write this line:


connect(lineedit1,SIGNAL(textChanged(const QString &)),this,SLOT(customSlot(const QString &)));


using pointers not references?

jacek
7th November 2006, 21:05
How can I write this line: [...] using pointers not references?
You can't, because QLineEdit emits textChanged( const QString& ), not textChanged( const QString * ).

eleanor
12th November 2006, 16:58
Hey. I've done everything as you've told me and it does not work.

This is the error:



QObject::connect: No such slot QLabel::customSlot(const QString&)
QObject::connect: (sender name: 'unnamed')
QObject::connect: (receiver name: 'unnamed')





#include <qapplication.h>
#include <qwidget.h>
#include "mojClass.h"

int main(int argc, char **argv)
{
QApplication a(argc,argv);
mojClass objekt;
a.setMainWidget(&objekt);
objekt.show();
a.exec();
}





#include <qwidget.h>
#include <qlineedit.h>
#include <qlabel.h>


class mojClass : public QWidget
{
Q_OBJECT
public:
mojClass();
private:
QLineEdit *lineedit1;
QLabel *label1;
QLabel *label2;
QLabel *label3;
QLabel *label4;
private slots:
void customSlot(const QString &input);
};




#include "mojClass.h"


mojClass::mojClass()
{
setGeometry(100,100,300,200);

lineedit1 = new QLineEdit(this);
lineedit1->setGeometry(110,10,50,20);
lineedit1->setEchoMode(QLineEdit::Normal);
lineedit1->setMaxLength(6);

label1 = new QLabel(this);
label1->setGeometry(10,10,100,20);
label1->setText("Enter something: ");

label2 = new QLabel(this);
label2->setGeometry(10,40,100,20);
label2->setText("You've entered: ");

label3 = new QLabel(this);
label3->setGeometry(120,70,100,20);

label4 = new QLabel(this);
label4->setGeometry(10,100,100,20);

connect(lineedit1,SIGNAL(textChanged(const QString &)),label4,SLOT(customSlot(const QString &)));

}

void mojClass::customSlot(const QString &input) {
int number = input.toInt() * 4;
label4->setNum(number);
}



Can you help me?

jpn
12th November 2006, 17:15
Did you remember to rerun qmake after adding the Q_OBJECT macro, as jacek told you to..? :)

jacek
12th November 2006, 17:30
connect(lineedit1,SIGNAL(textChanged(const QString &)),label4,SLOT(customSlot(const QString &)));
It should be:
connect( lineedit1, SIGNAL(textChanged(const QString &)), this, SLOT(customSlot(const QString &)) );
Because you have defined customSlot() slot in mojClass, not in QLabel.