sm3.142
26th September 2007, 22:35
Hi,
I wonder if anybody here knows of some background information about how something like the QtTcpSocket class works works with the event loop? The concept I'm trying to wrap my brain around is how to implement something like the readyRead() signal, respectively make sure that it's promptly emitted when there is data available. I've tried to look at the source, but it's a pretty bewildering heap of different classes, definitely not so easy to see through.
I guess the fundamental question could be phrased in even more generic terms: How does the main loop, in a single threaded application, ensure that every class can emit its signals when it decides it needs too? I've looked at a fair number of documents about event-driver programming, but haven't found this particular issue addressed anywhere. The common assumption in introductory texts seems to be that the external events originate in a single place, e.g. from the GUI API, e.g. key press, mouse click etc., and it's the main loop / event handler that collects them.
However, in the context of a concrete Qt example - e.g. QTcpSocket - you just instantiate this object, connect it's signals and more or less forget about it. And when the time comes it emits a readyRead signal sort of on its own. I'd have thought that it might be implemented with a timer inside the QSocket object, but I can't find any use of QTimer in the networking source code for the life of me, at least not pertaining to socket read.
The background is of course that I want to learn how to write my own classes that exhibit this sort of behavior, i.e. waiting for some external resource to become ready (without blocking the event loop) and then emit a signal when it finally does.
I feel right stupid because it seems this should somehow be obvious to me, but I just can't figure it out :confused: Maybe just a little shove in the right direction is needed...
many thanks
-stefan
I wonder if anybody here knows of some background information about how something like the QtTcpSocket class works works with the event loop? The concept I'm trying to wrap my brain around is how to implement something like the readyRead() signal, respectively make sure that it's promptly emitted when there is data available. I've tried to look at the source, but it's a pretty bewildering heap of different classes, definitely not so easy to see through.
I guess the fundamental question could be phrased in even more generic terms: How does the main loop, in a single threaded application, ensure that every class can emit its signals when it decides it needs too? I've looked at a fair number of documents about event-driver programming, but haven't found this particular issue addressed anywhere. The common assumption in introductory texts seems to be that the external events originate in a single place, e.g. from the GUI API, e.g. key press, mouse click etc., and it's the main loop / event handler that collects them.
However, in the context of a concrete Qt example - e.g. QTcpSocket - you just instantiate this object, connect it's signals and more or less forget about it. And when the time comes it emits a readyRead signal sort of on its own. I'd have thought that it might be implemented with a timer inside the QSocket object, but I can't find any use of QTimer in the networking source code for the life of me, at least not pertaining to socket read.
The background is of course that I want to learn how to write my own classes that exhibit this sort of behavior, i.e. waiting for some external resource to become ready (without blocking the event loop) and then emit a signal when it finally does.
I feel right stupid because it seems this should somehow be obvious to me, but I just can't figure it out :confused: Maybe just a little shove in the right direction is needed...
many thanks
-stefan