Results 1 to 4 of 4

Thread: My QTimer is not starting in another slot/function, but it's startin from constructor

  1. #1
    Join Date
    Apr 2011
    Location
    Hyderabad
    Posts
    86
    Thanks
    17
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default My QTimer is not starting in another slot/function, but it's startin from constructor

    Hi everybody,

    I am having one problem, i am giving you code can you find out where is problem.

    header file:
    Qt Code:
    1. class MainDialog : public QDialog
    2. {
    3. Q_OBJECT
    4. public:
    5. MainDialog();
    6. private:
    7. QLabel* labelText;
    8. QLabel* labelImage;
    9. QTimer *timer;
    10. QPushButton* startButton;
    11. QPushButton* quitButton;
    12. QSpacerItem* vspacer;
    13.  
    14. QStringList imagesStrList;
    15. int count;
    16. int listSize;
    17. private slots:
    18. void updateImages();
    19. void animateImage();
    20. };
    To copy to clipboard, switch view to plain text mode 

    src file:
    Qt Code:
    1. MainDialog::MainDialog() : QDialog()
    2. {
    3. QVBoxLayout *mainLayout = new QVBoxLayout(this);
    4. QHBoxLayout *hLayout = new QHBoxLayout;
    5.  
    6. labelText = new QLabel;
    7. labelText->setText("My Dialog ... !");
    8.  
    9. labelImage = new QLabel;
    10.  
    11. vspacer = new QSpacerItem(20, 150, QSizePolicy::Expanding, QSizePolicy::Minimum);
    12.  
    13. startButton = new QPushButton("Start");
    14. quitButton = new QPushButton("Quit");
    15.  
    16. hLayout->addWidget(startButton);
    17. hLayout->addWidget(quitButton);
    18.  
    19. mainLayout->addWidget(labelText);
    20. mainLayout->addItem(vspacer);
    21. mainLayout->addLayout(hLayout);
    22.  
    23. timer = new QTimer;
    24. imagesStrList << ":/images/1.png"
    25. << ":/images/2.png"
    26. << ":/images/3.png"
    27. << ":/images/4.png"
    28. << ":/images/5.png"
    29. << ":/images/6.png";
    30.  
    31. count = 0;
    32. listSize = imagesStrList.size();
    33.  
    34. connect(timer, SIGNAL(timeout()),this, SLOT(updateImages()));
    35. connect(startButton, SIGNAL(clicked()),this, SLOT(animateImage()));
    36. connect(quitButton, SIGNAL(clicked()),this, SLOT(close()));
    37.  
    38. //timer->start(400); //[COLOR="#0000FF"] it's working[/COLOR]
    39.  
    40. resize(320,240);
    41.  
    42. }
    43.  
    44. void MainDialog::updateImages()
    45. {
    46. qDebug("update");
    47. labelImage->setPixmap(QPixmap(imagesStrList.at(count++)));
    48. labelImage->show();
    49.  
    50. if(count>=listSize){
    51. count=0;
    52. }
    53.  
    54. }
    55.  
    56. void MainDialog::animateImage()
    57. {
    58. timer->start(400); //[COLOR="#FF0000"] It's not calling timer slot, I have to srart timer in this slot[/COLOR]
    59. for(int i=0; i<10; i++){
    60. qDebug("LOOP : %d",i);
    61. sleep(1);
    62. }
    63. timer->stop();
    64. }
    To copy to clipboard, switch view to plain text mode 

    main file:
    Qt Code:
    1. int main(int argc, char *argv[])
    2. {
    3. QApplication a(argc, argv);
    4.  
    5. Q_INIT_RESOURCE(resource);
    6.  
    7. MainDialog dlg;
    8. dlg.show();
    9.  
    10. return a.exec();
    11. }
    To copy to clipboard, switch view to plain text mode 

    i have to start my timer in different slot, but it's not calling the timer slot, if i am starting the timer in constructor, it's working fine.
    can you tel me where i am doing wrong ?

  2. #2
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: My QTimer is not starting in another slot/function, but it's startin from constru

    Because for loop in animateImage() method is blocking application event loop.
    If you want to do animation for a while you have to use second QTimer to stop it. Something like this :
    Qt Code:
    1. void MainDialog::animateImage()
    2. {
    3. timer->start(400); //[COLOR="#FF0000"] It's not calling timer slot, I have to srart timer in this slot[/COLOR]
    4. QTimer::singleShot(10000,timer,SLOT(stop()));//stop animation after 10 seconds
    5. }
    To copy to clipboard, switch view to plain text mode 

  3. The following user says thank you to Lesiok for this useful post:

    sanjeet (22nd November 2011)

  4. #3
    Join Date
    Apr 2011
    Location
    Hyderabad
    Posts
    86
    Thanks
    17
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: My QTimer is not starting in another slot/function, but it's startin from constru

    hi Lesiok,
    first of all thanks for suggesting me.
    But my problem is, i have to exeucute some busy process there, i dont know how much time exactly it will take to finish.
    So, i cant take second timer as single shot.
    I am writing down again that part of code.

    Qt Code:
    1. void MainDialog::animateImage()
    2. {
    3. timer->start(400); // It's not calling timer slot, I have to srart timer in this slot
    4.  
    5. some_busy_or_long_process(); // i dont know how much time it will take, may be 10 sec, 5 sec, 12 sec etc,
    6.  
    7. timer->stop();
    8. }
    To copy to clipboard, switch view to plain text mode 

    I have to start timer before execution of that busy process and i have to stop it after finishing that busy process.
    can you give me some idea.........?

  5. #4
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: My QTimer is not starting in another slot/function, but it's startin from constru

    Your program cannot service the timeout() signal unless your code returns to the event loop.

    You should read this: [wiki=Keeping_the_GUI_Responsive]Keeping the GUI Responsive[/wiki]

Similar Threads

  1. Replies: 2
    Last Post: 26th August 2011, 08:51
  2. QTimer not calling slot
    By been_1990 in forum Qt Programming
    Replies: 8
    Last Post: 25th October 2010, 14:31
  3. Connected QTimer slot not being called
    By Polnareff in forum Qt Programming
    Replies: 4
    Last Post: 27th July 2010, 15:55
  4. Replies: 3
    Last Post: 31st January 2010, 16:56
  5. In the QTimer Slot, the setStyleSheet Problem
    By nightrain in forum Qt Programming
    Replies: 3
    Last Post: 29th December 2009, 10:39

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.