
Originally Posted by
probine
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)));
string name = "hi";
connect(btnLogin, SIGNAL(clicked()), classB, SLOT(connectToServer(name)));
To copy to clipboard, switch view to plain text mode
Class B:
public slots:
void connectToServer(string);
public slots:
void connectToServer(string);
To copy to clipboard, switch view to plain text mode
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)))
connect(btnLogin, SIGNAL(clicked()), classB, SLOT(connectToServer(name)))
To copy to clipboard, switch view to plain text mode
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>
...
mapper
->setMapping
(btnLogin,
QString("hi!"));
connect(btnLogin, SIGNAL(clicked()), mapper, SLOT(map()))
connect(mapper, SIGNAL(mapped(const QString&)), classB, SLOT(connectToServer(const QString&)))
#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&)))
To copy to clipboard, switch view to plain text mode
Bookmarks