PDA

View Full Version : Slots in LineEdit



Misko
27th July 2007, 16:42
Hi,

I'm trying to 'connect' the value of a QSlider to a LineEdit's setText, but it doesn't work. On compiling there are no errors, but the value doesn't get into LineEdit...
Here is the code i've used:
#include <QApplication>
#include <QSlider>
#include <QLineEdit>
int main(int argc, char* argv[])
{
QApplication app(argc, argv);
QSlider *sldValue = new QSlider(Qt::Horizontal);
QLineEdit *leValue = new QLineEdit;
QObject::connect(sldValue, SIGNAL(valueChanged(int)), leValue, SLOT(setText("Test")));
// i just use the text 'Test' for now; this doesn't work also
// the widgets are then added to a layout
return app.exec();
}
With this example i just want to put the text 'Test' into LineEdit when the slider has changed of value. Can't be that hard? :confused:

I'm keeping the example very short, because i want this time to get QT right (i've abanded QT many times because i find it very difficult to learn)

Thanx in advance,
Misko

marcel
27th July 2007, 16:51
Slots don't work that way.
The signature(the parameters) of the signal must match the signature of the slot( but a slot can have fewer parameters than the signal, since it can ignore the extra ones).

You will eventually need to create a class for the main window and add a slot in your class( the following is the implementation of the slot):


void SomeClass::updateTextEdit(int value)
{
QString str = QString("%1").arg(value);
leValue.setText(str);
}
The line edit and the slider should be members in your class.
Then, you can connect like:


connect(sldValue, SIGNAL(valueChanged(int)), this, SLOT(updateTextEdit(int)));
This is pretty similar of what you want to do. It will set the current value of the slider in the line edit.

Regards

Misko
28th July 2007, 10:52
Hi Marcel,

Thx for your advice!
It took me some time to implement the class, because i'm still totally newbie with creating classes, but now it works!!!
1 question: in your reply you've put leValue.setText(str);
It gave me the error: request for member 'setText' in leValue, which is of non-class type 'QLineEdit'
Instead i've put leValue->setText(str); and it works, but don't ask me why? :eek:

Regards,
Misko

marcel
28th July 2007, 13:36
It works because leValue is a pointer.
I probably typed "." by mistake.

As a rule, remember that whenever you have a pointer to an object you can access its members with "->", as opposed to when you have the object allocated on the stack and you access them with "."( member selection by reference).

Regards