[Qt-interest] QThread question

BRM bm_witness at yahoo.com
Thu Dec 3 15:04:25 CET 2009


Well...you really should be doing this more Qt'ish...

for starters:

1) Create an QObject that has access to the network connection to do the actual work.
2) You run() should be something like:

- create object (#1) on heap (dynamic alloc)
- exec()
- destroy object

(You might be able to get away with static alloc.)

Otherwise your signals/slots won't work.

But you really don't need to do that kind of check with Qt either as QTcpSocket has signals for connect/disconnect:

void QAbstractSocket::connected()
void QAbstractSocket::disconnected()


And you could basically just use those to keep track of the socket coming and going instead of having to do all this work and an extra thread to ping it.
Now if your application level network protocol needs the ping, that is a different story - but then you can just setup a QTimer to emit that ping message
however often you need it.

Ben



________________________________
From: Tolik Piskov <tolik.piskov at gmail.com>
To: qt-interest at trolltech.com
Sent: Thu, December 3, 2009 8:18:37 AM
Subject: [Qt-interest] QThread question

I need to implement a thread that will be constantly checking network connection. It will look something like this:

class PingThread: public QThread 
{
public:
PingThread(QObject *parent);
void run();
signal:
        void ok();
        void fail();
}

...

void run()
{
while(true)
{
if ( ping() ) 
                   emit ok();
                else
                   emit fail();

Sleep(1000);
}
}

What I also going to do, is to stop and start this thread from my application several times. Usually I added bool variable bStop with default value 'false' and a signal to set it to 'true'. And then I simply checked its value in while() loop.

Is it appropriate or there's some qt-way I must read about?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20091203/ccfd8ef3/attachment.html 


More information about the Qt-interest-old mailing list