[Qt-interest] signals, sender(), and threads...
BRM
bm_witness at yahoo.com
Mon Nov 23 21:41:15 CET 2009
I have a network server program that I am porting from C/C++ to Qt to take advantage of Qt's threading, networking, and signal/slot functionality.
In the original program, each client was assigned its own handling thread.
The Qt version is using signals and slots and threads to handle the client communications, but some of the functionality is getting brought back to slots
in the main program (also okay, and actually resolves some other issues related to global information).
Each client connection has a QTcpSocket handled in a thread. The thread is has an instance object, which is run using a QThread derived class.
The thread uses signals/slots to communicate between things; and the instance object adds some extra functionality.
Presently, I have all the clients getting connected to the same slots and signals in the main program. And while this is good for some things, since it
makes it easy to broadcast messages to all clients; I don't want to broadcast messages to all clients all the time - just at certain times.
I was thinking that I could use sender() to find who sent it; but then remembered reading in the Qt documentation on QObject::sender() that there
are some instances where sender() returns an invalid object[1]. Which leads me to the following questions:
1) The QTcpSocket derived class only has its signals/slots connected to the instance object; the instance object to the QThread class's signals, and the
QThread class to the main program's signals/slots. Is this a situation where the sender() would be invalid? Or would it return the QThread object as the sender?
2) Is there a better way to do what I am trying to do? I suspect that there is a better Qt-way to do this; but am not sure.
TIA,
Ben
[1]http://doc.trolltech.com/4.5/qobject.html#sender
More information about the Qt-interest-old
mailing list