PDA

View Full Version : QObject::connect: Cannot queue arguments of type 'QString&'



Dumbledore
9th November 2007, 18:28
Unlike the typical client/server situation, my chat application is both a client and a server. It is a server because it runs an instance of qtcpserver and it is a client, because you can make connections using qtcpsocket.

To have multiple connections i assumed you need multithreading although I have no experience with multithreading+ I'm new to QT.

Here is how I'm implementing the ability to establish a connection and then pass off that connection to its own thread.



// background info
MSocket tcpSocket;

connect(&tcpSocket, SIGNAL(connected()),
this, SLOT(connectionEstablished()));

tcpSocket.connectToHost(remoteIP, remotePort);

// end background.

void MChat::connectionEstablished() {
chatOutput->append("<font color=green>Connection Established.</font>");
MSocketThread* mSockThread = new MSocketThread(tcpSocket.socketDescriptor());
connect(this, SIGNAL(sendText(QString&)),
mSockThread, SIGNAL(sigSend(QString&)));
connect(mSockThread, SIGNAL(relayIncomingText(QString&)),
this, SLOT(outputText(QString&)));
connect(mSockThread, SIGNAL(socketError()),
this, SLOT(error()));
connect(mSockThread, SIGNAL(finished()),
mSockThread, SLOT(deleteLater()));
mSockThread->start();

}




Here's how I'm implementing the MSocketThread




void MSocketThread::run() {
MSocket mSocket;
if ( !mSocket.setSocketDescriptor(socketDescriptor) ) {
emit error(mSocket.error());
return;
}
connect(&mSocket, SIGNAL(inboundText(QString&)),
this, SIGNAL(relayIncomingText(QString&)));
connect(this, SIGNAL(sigSend(QString&)),
&mSocket, SLOT(send(QString&)));
/*
connect(mSocket, SIGNAL(error(QAbstractSocket::SocketError)),
this, SIGNAL(socketError()));
*/
mSocket.waitForDisconnected();


}



Finally, here is the problem I'm encountering. When I try to send some chat, the console window displays this message:


QObject::connect: Cannot queue arguments of type 'QString&'
(Make sure 'QString&' is registed using qRegisterMetaType().)


And no chat gets displayed on the remote chat app. output.

Any tips, solutions, etc. are welcome as I am very new.

marcel
9th November 2007, 18:33
To have multiple connections i assumed you need multithreading although I have no experience with multithreading+ I'm new to QT.

Threading is not something specific to Qt.

As for the connect problem just pass a QString not a QString& because it is an implicitly shared class, therefore no overhead appears.

Dumbledore
9th November 2007, 18:45
Removing all the references it no longer displays that message, but I'm still unable to see the chat I'm receiving (if i'm receiving it at all).

I'd also really like comments on my architecture. Please point out anything I'm doing that is unnecessary or wrong.


Threading is not something specific to Qt.
That's true, I simply said I am new to both QT and multithreading. So expect mistakes!

jpn
9th November 2007, 23:15
Passing a non-const reference parameter suggests that you want the receiving function to be able to modify the value and that the modification will affect the passed parameter value. Of course, this is something you cannot do when working with signals and slots across threads. Behind the scenes, parameters, a QString in this case, will be serialized and delivered in a custom event across thread boundaries. Thus, making it non-const reference does not make sense.


I'd also really like comments on my architecture. Please point out anything I'm doing that is unnecessary or wrong.
Thanks to the fact that QTcpSocket works asynchronously I'm in serious doubt that you even need an external thread to handle data flow.