[Qt-interest] QThread Problem

alexander golks alex at golks.de
Fri Feb 12 14:35:32 CET 2010


perhaps main.cpp-attachment will give you some hints, based on your code.

basically, a QThread::run() method may look like:

  void my_thread::run()
  {
    while(running){
      // do hard work
      this->display();
    }
  }

where running should be a volatile bool.
you may use my_thread::start() to set running to true and QThread::start()'ing the thread,
and my_thread::stop() to set running to false, and let the run() method stop, thus the thread ends.

with this, you just do:

  // somewhere in your code
  my_thread* athread=new my_thread();
  athread->start();

  // somewhere different in your code
  athread->stop();
  athread->wait();  // you should wait, until your thread really quits
  delete athread;

more work needs to be done, if your seperate thread needs an own eventloop. therefore the my_thread::run()
method should once call exec() to start the eventloop. but that would be another funny story about threading.

and possibly some more hints on:
http://doc.trolltech.com/4.6/threads.html
http://doc.trolltech.com/4.6/examples-threadandconcurrent.html


alex

-- 
/*
 *  printk("3c505 is sulking\n");
 *          linux-2.6.6/drivers/net/3c505.c
 */
-------------- next part --------------
A non-text attachment was scrubbed...
Name: main.cpp
Type: text/x-c++src
Size: 1146 bytes
Desc: not available
Url : http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20100212/e12e370d/attachment.bin 
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 197 bytes
Desc: not available
Url : http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20100212/e12e370d/attachment-0001.bin 


More information about the Qt-interest-old mailing list