Results 1 to 9 of 9

Thread: QTimer not calling slot

  1. #1
    Join Date
    Oct 2008
    Posts
    306
    Thanks
    6
    Thanked 9 Times in 8 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default QTimer not calling slot

    I have a QObject subclass that sets a timer to call a slot. But that never happens.

    shot.h:
    Qt Code:
    1. #ifndef SHOT_H
    2. #define SHOT_H
    3.  
    4. #include <QObject>
    5. #include <QTimer>
    6.  
    7.  
    8. class Shot : public QObject
    9. {
    10. Q_OBJECT
    11. public:
    12. explicit Shot(QObject *parent = 0);
    13. private:
    14. QTimer *timer;
    15. public slots:
    16. void updatePos();
    17.  
    18. };
    19.  
    20. #endif // SHOT_H
    To copy to clipboard, switch view to plain text mode 

    shot.cpp:
    Qt Code:
    1. #include "shot.h"
    2. #include <QDebug>
    3.  
    4. Shot::Shot(QObject *parent) :
    5. QObject(parent)
    6. {
    7. timer = new QTimer();
    8. connect(timer, SIGNAL(timeout()), this, SLOT(updatePos()));
    9. timer->setInterval(0);
    10. timer->start();
    11. }
    12.  
    13. void Shot::updatePos()
    14. {
    15. //timer never reaches here...
    16. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: QTimer not calling slot

    Does the slot really exist? Is the file processed by moc?
    Last edited by tbscope; 24th October 2010 at 20:02.

  3. #3
    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: QTimer not calling slot

    Is the event loop running?
    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.


  4. #4
    Join Date
    Oct 2008
    Posts
    306
    Thanks
    6
    Thanked 9 Times in 8 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QTimer not calling slot

    Does the slot really exist? Is the file processed by moc?
    Yes, it does:
    Qt Code:
    1. public slots:
    2. void updatePos()
    To copy to clipboard, switch view to plain text mode 
    ;

    Is the event loop running?
    Hmmm.. I think so. This is a class I call to create an item when needed.
    Qt Code:
    1. if(btn1){
    2. ui->lineEdit->setText("cliked");
    3. Shot tiro;
    4. }
    To copy to clipboard, switch view to plain text mode 

  5. #5
    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: QTimer not calling slot

    Think what is the scope of your "tiro" object and what happens to it when line #4 of the code is reached.
    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.


  6. #6
    Join Date
    Oct 2008
    Posts
    306
    Thanks
    6
    Thanked 9 Times in 8 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QTimer not calling slot

    @#!!%&* I should really take a shot on the head.

    Scope, I always forget.... Thanks for the patience.

    BTW, why is QGraphicsView so slow when updating an item's position?

  7. #7
    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: QTimer not calling slot

    Quote Originally Posted by been_1990 View Post
    BTW, why is QGraphicsView so slow when updating an item's position?
    Because you are not using it correctly.
    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.


  8. #8
    Join Date
    Oct 2008
    Posts
    306
    Thanks
    6
    Thanked 9 Times in 8 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QTimer not calling slot

    I'm using setPos(x,y) to update it's position. Isn't that correct? I checked out the "40000 Chips" example, and copied some settings from it's QGraphicsView:
    Qt Code:
    1. graphicsView->setOptimizationFlags(QGraphicsView::DontSavePainterState);
    2. graphicsView->setViewportUpdateMode(QGraphicsView::SmartViewportUpdate);
    To copy to clipboard, switch view to plain text mode 
    But it still goes slow. Where can I see an example of correct implementation of QGraphicsView with changing elements positions(not drag-'n-drop)

  9. #9
    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: QTimer not calling slot

    There is no correct implementation of Graphics View bla bla bla. What is correct depends on a case-by-case basis.
    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. Calling a slot from another thread
    By pherthyl in forum Qt Programming
    Replies: 2
    Last Post: 19th March 2010, 22:13
  2. Differences in calling a Slot
    By Denarius in forum Qt Programming
    Replies: 7
    Last Post: 28th April 2009, 15:32
  3. Calling a slot with arguments
    By drhex in forum Qt Programming
    Replies: 6
    Last Post: 1st May 2008, 21:22
  4. menu actions calling a single slot
    By jayw710 in forum Qt Programming
    Replies: 2
    Last Post: 28th June 2007, 19:19
  5. Replies: 5
    Last Post: 6th March 2007, 05:34

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.