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?