Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Slot connected to signal emitted from thread does not get called
Qt 6.11 is out! See what's new in the release blog

Slot connected to signal emitted from thread does not get called

Scheduled Pinned Locked Moved Unsolved General and Desktop
3 Posts 3 Posters 878 Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • michalt38M Offline
    michalt38M Offline
    michalt38
    wrote on last edited by michalt38
    #1

    I have defined a new slot in a class that inherits from QThread:

    class MyThread : public QThread
    {
        Q_OBJECT
    
    public:
        MyThread(QString fileName);
        void run() override;
    
    signals:
        void addNewItem(int row, int col, QString text);
    
    private:
        QString fileName;
    };
    

    In the Dialog class I have defined slot that should get called when the above signal will be emitted. This slot updates widget on UI:

    void Dialog::onAddNewItem(int row, int col, QString text)
    {
        ui->tableWidget->setItem(row, col, new QTableWidgetItem(text));
    }
    

    In the constructor of the Dialog class I have created the working thread and connected signal to a slot:

    worker = new MyThread(filePath);
    worker->run();
    
    connect(worker, &MyThread::addNewItem, this, &Dialog::onAddNewItem);
    

    In the run() function I'm emitting the addNewItem signal but the onAddNewItem slot never gets called. Why is that?

    CharbyC 1 Reply Last reply
    0
    • michalt38M michalt38

      I have defined a new slot in a class that inherits from QThread:

      class MyThread : public QThread
      {
          Q_OBJECT
      
      public:
          MyThread(QString fileName);
          void run() override;
      
      signals:
          void addNewItem(int row, int col, QString text);
      
      private:
          QString fileName;
      };
      

      In the Dialog class I have defined slot that should get called when the above signal will be emitted. This slot updates widget on UI:

      void Dialog::onAddNewItem(int row, int col, QString text)
      {
          ui->tableWidget->setItem(row, col, new QTableWidgetItem(text));
      }
      

      In the constructor of the Dialog class I have created the working thread and connected signal to a slot:

      worker = new MyThread(filePath);
      worker->run();
      
      connect(worker, &MyThread::addNewItem, this, &Dialog::onAddNewItem);
      

      In the run() function I'm emitting the addNewItem signal but the onAddNewItem slot never gets called. Why is that?

      CharbyC Offline
      CharbyC Offline
      Charby
      wrote on last edited by
      #2

      @michalt38 Hi, you might want to check the excellent checklist made by Giuseppe D'Angelo : https://www.kdab.com/slot-not-invoked/ . For instance, you could test the connect to return true or that the connect does not gets deleted following thread destruction...

      1 Reply Last reply
      1
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #3

        Hi,

        One thing: to start your thread, call the start method, run will be called automatically in the new thread. Here, you will have your run method executed and then you connect the signal. Hence the slot will never be called.

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        4

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved