Results 1 to 7 of 7

Thread: Correct usage of QThread

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Nov 2010
    Posts
    315
    Thanked 53 Times in 51 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Correct usage of QThread

    Rename InventoryThread to InventoryWorker or something similar.
    You really don't need QThreadEx.

    Qt Code:
    1. MainWindow::MainWindow(QWidget *parent) :
    2. QMainWindow(parent),
    3. ui(new Ui::MainWindow)
    4. {
    5. ui->setupUi(this);
    6.  
    7. invThread = new QThread(this);
    8. worker = new InventoryWorker; // parent is forbidden here
    9. worker->moveToThread(invThread);
    10. connect(ui->start,SIGNAL(clicked()),
    11. invThread, SIGNAL(startTimer())); // add slot startTimer in InventoryWorker
    12. invThread->start();
    13. }
    14.  
    15. MainWindow::~MainWindow() {
    16. worker->deleteLater();
    17. invThread->terminate();
    18. invThread->waitFor(3000);
    19. }
    20.  
    21. InventoryWorker::InventoryWorker(QObject *parent, int h, QListWidget * lst) : QObject(parent)
    22. {
    23. handle = h;
    24. list = lst;
    25. timer = new QTimer(this);
    26. list->connect(timer,SIGNAL(timeout()),SLOT(clear()));
    27. connect(timer,SIGNAL(timeout()),list,SLOT(clear()));
    28. timer->setSingleShot(false);
    29. }
    30.  
    31. InventoryWorker::startTimer() {
    32. timer->start(1500);
    33. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Nov 2010
    Posts
    19
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Correct usage of QThread

    How does your code call InventoryWorker:: DoInventory()? To put it simply what I need is:

    every time ui->start emits clicked() then run InventoryWorker:: DoInventory() in a different thread

  3. #3
    Join Date
    Nov 2010
    Posts
    315
    Thanked 53 Times in 51 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Correct usage of QThread

    This code I gave you is not complete. I wrote it just to show you a pattern. I don't know what you code do I don't need to.

  4. #4
    Join Date
    Nov 2010
    Posts
    19
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Correct usage of QThread

    Ok, after reading it twice I got it. Tank you all, you've been really helpful

Similar Threads

  1. Is this sentence correct?
    By SWEngineer in forum Newbie
    Replies: 6
    Last Post: 21st June 2011, 01:56
  2. Replies: 1
    Last Post: 9th May 2011, 13:16
  3. QThread 100% cpu usage on linux only
    By essial in forum Qt Programming
    Replies: 6
    Last Post: 26th April 2010, 15:33
  4. QThread usage and exec()
    By smahnken in forum Qt Programming
    Replies: 10
    Last Post: 24th August 2008, 18:13
  5. CPU Time & Memory Usage in QThread or QProcess
    By Davidaino in forum Qt Programming
    Replies: 0
    Last Post: 11th July 2008, 19:15

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.