Results 1 to 6 of 6

Thread: QThread & QTimer

  1. #1
    Join Date
    Apr 2009
    Posts
    16
    Qt products
    Qt4
    Platforms
    Unix/X11

    Question QThread & QTimer

    Hi every body, I'm trying to make a window and show a time in every 1 minutes. This is what what I wrote to do that:

    MyThread .h file:
    Qt Code:
    1. class MyThread : public QThread
    2. {
    3. Q_OBJECT
    4. private:
    5. MainWindow *w;
    6. QTimer *timer;
    7. public:
    8. MyThread( QObject* parent );
    9. void run();
    10. public slots:
    11. void ToDo();
    12. };
    To copy to clipboard, switch view to plain text mode 

    main.cpp
    Qt Code:
    1. MainWindow mw;
    2. MyThread::MyThread( QObject* parent ) : QThread( parent )
    3. {
    4. // nothing to do !!!
    5. }
    6.  
    7. void MyThread::run()
    8. {
    9. mw.show();
    10. timer = new QTimer(this);
    11. connect(timer,SIGNAL(timeout()), this, SLOT(ToDo()));
    12. timer->setInterval(2009);
    13. timer->start();
    14. return a.exec();
    15. }
    16. void MyThread::ToDo() {
    17. long i=0,h=0,m=0,s=0;
    18. while(i<3)
    19. {
    20. std::string str = "Time: ";
    21. char msg[100];
    22. Converter(gTime,h,m,s);
    23. sprintf(msg,"%02ld:%02ld:%02ld",h,m,s);
    24. str += msg;
    25. w->setText(QString::fromUtf8(str.c_str(),str.size()));
    26. sleep(1);
    27. i++;
    28. gTime--;
    29. }
    30. a.closeAllWindows();
    31. }
    32.  
    33. int main()
    34. {
    35. MyThread mt(0);
    36. mt.start();
    37. mt.wait();
    38. }
    To copy to clipboard, switch view to plain text mode 

    It is compiled but this is the error when I try to execute the program:

    QObject: Cannot create children for a parent that is in a different thread.
    (Parent is MyThread(0xbfbc4cac), parent's thread is QThread(0x80843a0), current thread is MyThread(0xbfbc4cac)
    Thanks for your consideration

  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 & QTimer

    What is the thread for?
    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.


  3. #3
    Join Date
    Apr 2009
    Posts
    16
    Qt products
    Qt4
    Platforms
    Unix/X11

    Question Re: QThread & QTimer

    mmmmmmm, I don't know !!!!!
    This is what I exactly want to do:

    Program starts and shows a timer for 5 seconds. Then it hides or closes and after 10 minutes shows the same. What I have to do?

    I tried to make a QThread and show the mainwindow, then set a QTimer, it has to change the text of a label for a 5 second timer.

    Any better way?!

  4. #4
    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 & QTimer

    You don't need the thread. The timer will let you know when a specified amount of time has passed. Here is a sample code:
    Qt Code:
    1. #include <QApplication>
    2. #include <QLabel>
    3. #include <QTimer>
    4. #include <QDateTime>
    5.  
    6. class Dummy : public QObject {
    7. Q_OBJECT
    8. public:
    9. Dummy(QLabel *l) : QObject(){m_label = l; }
    10. public slots:
    11. void doIt(){ m_label->setText(QDateTime::currentDateTime().toString()); }
    12. private:
    13. QLabel *m_label;
    14. };
    15.  
    16. #include "main.moc"
    17.  
    18. int main(int argc, char **argv){
    19. QApplication app(argc, argv);
    20. QLabel label;
    21. QTimer t;
    22. Dummy dummy(&label);
    23. dummy.doIt();
    24. QObject::connect(&t, SIGNAL(timeout()), &dummy, SLOT(doIt()));
    25. t.start(1000);
    26. label.show();
    27. return app.exec();
    28. }
    To copy to clipboard, switch view to plain text mode 
    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.


  5. #5
    Join Date
    Apr 2009
    Posts
    16
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QThread & QTimer

    You are right but I want to hide the window after 10 seconds for 5 minutes then show it again. What now?

    Thanks again & again & ...

  6. #6
    Join Date
    Feb 2008
    Location
    Russia, Moscow
    Posts
    35
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QThread & QTimer

    Uff.. The problem is solved... Thanks to all!

Similar Threads

  1. Replies: 8
    Last Post: 27th March 2013, 11:51
  2. QTimer in QThread doesn't start or timeout
    By Boron in forum Qt Programming
    Replies: 9
    Last Post: 21st October 2011, 13:51
  3. QTimer and QThread in Qtopia 4.2.0
    By mellibra in forum Qt for Embedded and Mobile
    Replies: 3
    Last Post: 25th October 2007, 08:26
  4. QTimer and QThread
    By TheKedge in forum Qt Programming
    Replies: 4
    Last Post: 21st September 2006, 14:52
  5. Replies: 6
    Last Post: 17th March 2006, 17:48

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.