[Qt-interest] QTcpSocket timeouts
Bob Hood
bhood2 at comcast.net
Thu Mar 18 04:00:33 CET 2010
Anatoly Burakov wrote:
> On 18.03.2010 2:37, Bob Hood wrote:
>> You probably need to let the event loop run so that data from the
>> incoming connection can be processed and queued up for you. That's
>> essentially what is happening when you post the dialog box (i.e., and
>> event loop runs and data is processed).
>>
> Just tried to put qApp->processEvents() instead of message boxes -
> didn't help. Any other ideas?
Yes. Following Qt's programming model would probably help a lot.
That means you need something event-driven, so the event loop runs and
you respond to events it generates. Just as a rough example, you need
to have a class (say, "SocketEvent") that has slots that can receive
signals generated by your sockets. Hook things up, and then run the
event loop.
For example, if you are writing a console app, you could do something
like this (minimal and untested):
int main(int argc, char* argv[])
{
...
QCoreApplication app(argc, argv);
// set up sockets
QTcpSocket* my_socket = new QTcpSocket();
<further setup here>
// create my signal-processing class instance
SocketEvent* my_socket_event = new SocketEvent(my_socket);
// connect signals to slots
QObject::connect(my_socket_event, SIGNAL(readyRead()),
my_socket_event, SLOT(read_device()));
<further setup here>
// run the Qt event loop
int result = app.exec();
// clean up
QObject::disconnect(my_socket_event, SIGNAL(readyRead()),
my_socket_event, SLOT(read_ready()));
<further cleanup here>
delete my_socket_event;
// exit
return result;
}
Qt will then perform the data processing and queuing for you within the
event loop, and your processing class ("SocketEvent") will receive
notification that data is ready without having to block and busy wait
things.
More information about the Qt-interest-old
mailing list