PDA

View Full Version : passing arguments in SLOT



eleanor
3rd July 2008, 21:05
Hi, I'm having problem passing arguments in SLOT:


private slot:
void connectToHost(QString, quint32);

and I'm calling it like this:


connect(connectButton, SIGNAL(clicked()), this, SLOT(connectToHost(host, port)));

while the connectToHost is:


void MainWindow::connectToHost(QString host, quint32 port) {
/* create and connect to the socket */
socket = new QTcpSocket(this);
socket->connectToHost(host, port, QIODevice::ReadOnly);
}

Problem is:

Object::connect: No such slot MainWindow::connectToHost(host,port)

janus
3rd July 2008, 21:23
Hi,

isn't it "private slots: ?

eleanor
3rd July 2008, 21:27
Yes it is...I make a mistake typing....

I have slots in my code.

caduel
3rd July 2008, 21:55
You don't call a slot with connect.

You create a connection to between a signal and a slot with connect.
Every time that signal is emitted (with its arguments) the slots is called with those.

If you want to call a 'slot', just do it. It is a regular function.

When connecting you must not use names of some variables, but types:


// does not quite work
connect(connectButton, SIGNAL(clicked()), this, SLOT(connectToHost(QString, quint32)));

This does not work either, as the slot's arguments must be those (or less) of the signal.
clicked() has no arguments, so neither can the slot you connect to.



// perhaps in the constructor:
connect(connectButton, SIGNAL(clicked()), this, SLOT(doConnect()));

MainWindow::doConnect()
{
connectToHost(host, port);
}

where host and port would be members of MainWindow, and doConnect a slot in MainWindow.
connectToHost can be a slot, but does not have to, since you're only connecting to doConnect() here.

HTH

PS: Do read the excellent Qt docs and tutorials on the subject!