Results 1 to 2 of 2

Thread: QThread don' stop (or the signal don't emit)

  1. #1
    Join Date
    Mar 2011
    Posts
    2
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11

    Default QThread don' stop (or the signal don't emit)

    Hi (sorry for my english).. i'm new in the forum, and this is my first post,(shame.. is a question) .. I'm working in a console application that use OpenGL to show a Camera view, and i add Qt to use the really awesome class QThread (C++) ..But i have some problems.. here my code and then the description:
    Qt Code:
    1. #ifndef CONTADOR_REGISTER_H
    2. #define CONTADOR_REGISTER_H
    3.  
    4. #include <QThread>
    5. #include <QObject>
    6. #include <QDebug>
    7.  
    8. #include "database.h"
    9.  
    10. class ContadorRegister : public QThread
    11. {
    12. Q_OBJECT
    13. public:
    14. ContadorRegister(User user, UserID id);
    15. void run();
    16. private:
    17. User user;
    18. UserID uid;
    19. public slots:
    20. void fin(){ qDebug()<<"Thread ended"; }
    21.  
    22. };
    23.  
    24. #endif // CONTADOR_REGISTER_H
    25.  
    26. //And the implementation in contador_register.cpp
    27. #include "contador_register.h"
    28.  
    29. ContadorRegister::ContadorRegister(const User userG, UserID id){
    30. this->uid = id;
    31. this->user = userG;
    32. }
    33.  
    34.  
    35. void ContadorRegister::run(){
    36. this->setTerminationEnabled(true);
    37. QObject::connect(this,SIGNAL(finished()),this,SLOT(fin()));
    38. int dir=0;
    39. //Some calculations
    40. //Registro en BD
    41. if(dir==1){
    42. QString sql="INSERT INTO registro VALUES(";
    43. sql.append("NOW())");
    44. DB->insertar(sql);
    45. }
    46. this->finished();
    47. }
    To copy to clipboard, switch view to plain text mode 

    And finally the function where i call this thread.
    This is a callback function .. this function trigger up every time of a user is saw for the camera.

    Qt Code:
    1. ContadorRegister* cr = new ContadorRegister(generator,nId);
    2. cr->start();
    3. qDebug()<<"Starting" << cr->currentThreadId();
    4.  
    5. if(cr->isRunning()){
    6. qDebug()<<"Still Running "<<cr->currentThreadId();
    7. }else
    8. {
    9. qDebug()<<"Ended "<<cr->currentThreadId();
    10. }
    To copy to clipboard, switch view to plain text mode 

    Well .. the problem is.. i this function (or another in the main) i need to check if the QThread is running or not...

    And beside.. if the thread don't die.. the memory consume will be big... this application will run 24hrs 7days..

    Any idea?

    Thanks

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QThread don' stop (or the signal don't emit)

    First of all currentThreadId() returns something else than you think. As the name suggests it returns the CURRENT THREAD id, not the id of the thread you're calling the method on (it's a static method so the caleee instance is irrelevant). Second of all if your callback function is represented by the second snippet of yours then it will not work and it doesn't have sense to be working in this configuration. If you spawn a thread and do nothing while waiting for it to finish (assuming you would actually wait in your code and not just check if the thread was running) then there is no benefit in having the thread in the first place.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. Moving QObject to QThread causes signals to stop working
    By Ban-chan in forum Qt Programming
    Replies: 8
    Last Post: 13th July 2010, 21:39
  2. How to stop QThread?
    By vespasianvs in forum Qt Programming
    Replies: 3
    Last Post: 14th March 2010, 06:42
  3. [QThread] Function calling after thread.stop()
    By Macok in forum Qt Programming
    Replies: 4
    Last Post: 7th February 2009, 13:33
  4. Replies: 1
    Last Post: 10th October 2007, 10:11
  5. QThread exec proplem to stop...
    By patrik08 in forum Qt Programming
    Replies: 29
    Last Post: 21st May 2007, 07:51

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.