PDA

View Full Version : Signals and Slots between 2 classes



probine
31st March 2006, 00:13
Class A has a QPushButton. Class B has a Slot called conectToServer().

Class A has to send an string called name, when the button is pressed to the Slot in class B.

Class A:


string name = "hi";
connect(btnLogin, SIGNAL(clicked()), classB, SLOT(connectToServer(name)));


Class B:


public slots:
void connectToServer(string);


When I use the same code, but I do not send any arguments in the call to connectToServer(), then everything is fine. Sending the string is not possible.

I read the docs and it says that a SLOT is like an ordinary C++ function, so it should be able to accept arguments.

Am I doing something wrong ?

Chicken Blood Machine
31st March 2006, 01:34
Class A has a QPushButton. Class B has a Slot called conectToServer().

Class A has to send an string called name, when the button is pressed to the Slot in class B.

Class A:


string name = "hi";
connect(btnLogin, SIGNAL(clicked()), classB, SLOT(connectToServer(name)));


Class B:


public slots:
void connectToServer(string);


When I use the same code, but I do not send any arguments in the call to connectToServer(), then everything is fine. Sending the string is not possible.

I read the docs and it says that a SLOT is like an ordinary C++ function, so it should be able to accept arguments.

Am I doing something wrong ?


Read the docs again, you cannot do this:


connect(btnLogin, SIGNAL(clicked()), classB, SLOT(connectToServer(name)))

Since a connect statment cannot contain parameters for the signals or slots. What you need to do is use an intermediate slot that will call your connectToServer() slot with the string parameter, or use a QSignalMapper, like this:


#include <QSignalMapper>
...

QSignalmapper* mapper = new QSignalMapper(this);
mapper->setMapping(btnLogin, QString("hi!"));
connect(btnLogin, SIGNAL(clicked()), mapper, SLOT(map()))
connect(mapper, SIGNAL(mapped(const QString&)), classB, SLOT(connectToServer(const QString&)))