Results 1 to 2 of 2

Thread: QTimer Not Triggering Slot in Qt Application

  1. #1
    Join Date
    Oct 2025
    Posts
    2
    Qt products
    Platforms
    Windows

    Default QTimer Not Triggering Slot in Qt Application

    Hi everyone,

    I’m having trouble with a `QTimer` that doesn’t seem to trigger its slot. The timer starts without errors, but the timeout function is never called.

    Here is a simplified version of my code:

    Qt Code:
    1. #include <QApplication>
    2. #include <QMainWindow>
    3. #include <QTimer>
    4. #include <QDebug>
    5.  
    6. class MainWindow : public QMainWindow
    7. {
    8. Q_OBJECT
    9.  
    10. public:
    11. MainWindow(QWidget *parent = nullptr)
    12. : QMainWindow(parent)
    13. {
    14. QTimer *timer = new QTimer(this);
    15. connect(timer, &QTimer::timeout, this, &MainWindow::onTimeout);
    16. timer->start(1000); // 1 second
    17. }
    18.  
    19. private slots:
    20. void onTimeout()
    21. {
    22. qDebug() << "Timer triggered";
    23. }
    24. };
    25.  
    26. int main(int argc, char *argv[])
    27. {
    28. QApplication a(argc, argv);
    29. MainWindow w;
    30. w.show();
    31. return a.exec();
    32. }
    To copy to clipboard, switch view to plain text mode 

    The application runs fine, but "Timer triggered" never appears in the console.
    Am I missing something related to the event loop or object lifetime?
    Is there something wrong with how I’m creating or connecting the timer?

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

    Default Re: QTimer Not Triggering Slot in Qt Application

    The code looks okay. Does this simplified version also not work for you?
    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. Replies: 2
    Last Post: 21st August 2019, 15:57
  2. Replies: 4
    Last Post: 3rd July 2018, 00:15
  3. Triggering the completer codewise
    By Sytse in forum Qt Programming
    Replies: 0
    Last Post: 2nd March 2016, 14:43
  4. Replies: 1
    Last Post: 25th October 2012, 20:47
  5. Replies: 15
    Last Post: 4th August 2012, 20:11

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.