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. how can I create a signal to use from inside an rt_task?
Qt 6.11 is out! See what's new in the release blog

how can I create a signal to use from inside an rt_task?

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 3 Posters 1.9k Views 1 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.
  • S Offline
    S Offline
    spiderdab
    wrote on last edited by
    #1

    I have a rt_task which runs on a xenomai kernel. I wanted to add a GUI, for it so I created a MainWindow project in qtcreator (qt5) and started playing with it.
    The problem I have is that when something happens inside my rt_task, I have to change something in the GUI, and I know the correct way is to use signal/slot, but I don't know how to create or emit a signal from a rt_thread.
    in the main.cpp the relevant part is this:

    void ethercat_process_run(void *arg)
    {
        RTIME now, previous=0;
        int32_t toff=0;
        RTIME rt_ts = rt_timer_read();
    
        while (run)
        {
    ...bla bla...
        }
    }
    
    int main(int argc, char *argv[])
    {
    ...cut...
        rt_task_create(&ethercat_task, "ethercat_proccess_task", 0, 99, 0);
        rt_task_set_affinity(&ethercat_task, &cpu_set_ecat); 		//CPU affinity forEtherCAT task
        rt_task_start(&ethercat_task, &ethercat_process_run, NULL);
    ...cut...
        QApplication a(argc, argv);
        MainWindow w;
        w.show();
    
        return a.exec();
    }
    
    1 Reply Last reply
    0
    • VRoninV Offline
      VRoninV Offline
      VRonin
      wrote on last edited by VRonin
      #2

      Signals are public methods. Just create a QObject containing your signals and call those methods.

      class rt_taskMessanger : public QObject{
      Q_OBJECT
      Q_DISABLE_COPY(rt_taskMessanger)
      public:
      explicit rt_taskMessanger(QObject* parent = nullptr) : QObject(parent){}
      signals:
      void somethingHappened();
      };
      
      rt_taskMessanger * rtMessanger;
      int main(int argc, char *argv[])
      {
      rt_taskMessanger msg;
      rtMessanger = &msg;
      ...cut...
          rt_task_create(&ethercat_task, "ethercat_proccess_task", 0, 99, 0);
          rt_task_set_affinity(&ethercat_task, &cpu_set_ecat); 		//CPU affinity forEtherCAT task
          rt_task_start(&ethercat_task, &ethercat_process_run, NULL);
      ...cut...
          QApplication a(argc, argv);
          MainWindow w;
          w.show();
      QObject::connect(rtMessanger,&rt_taskMessanger::somethingHappened,[](){qDebug("Something Happened");});
          return a.exec();
      }
      
      void ethercat_process_run(void *arg)
      {
          RTIME now, previous=0;
          int32_t toff=0;
          RTIME rt_ts = rt_timer_read();
      
          while (run)
          {
      ...bla bla...
      rtMessanger->somethingHappened();
          }
      }
      

      P.S.
      make sure your while (run) doesn't block the main event loop otherwise you'll probably need to move that part to a new thread

      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
      ~Napoleon Bonaparte

      On a crusade to banish setIndexWidget() from the holy land of Qt

      S 1 Reply Last reply
      4
      • VRoninV VRonin

        Signals are public methods. Just create a QObject containing your signals and call those methods.

        class rt_taskMessanger : public QObject{
        Q_OBJECT
        Q_DISABLE_COPY(rt_taskMessanger)
        public:
        explicit rt_taskMessanger(QObject* parent = nullptr) : QObject(parent){}
        signals:
        void somethingHappened();
        };
        
        rt_taskMessanger * rtMessanger;
        int main(int argc, char *argv[])
        {
        rt_taskMessanger msg;
        rtMessanger = &msg;
        ...cut...
            rt_task_create(&ethercat_task, "ethercat_proccess_task", 0, 99, 0);
            rt_task_set_affinity(&ethercat_task, &cpu_set_ecat); 		//CPU affinity forEtherCAT task
            rt_task_start(&ethercat_task, &ethercat_process_run, NULL);
        ...cut...
            QApplication a(argc, argv);
            MainWindow w;
            w.show();
        QObject::connect(rtMessanger,&rt_taskMessanger::somethingHappened,[](){qDebug("Something Happened");});
            return a.exec();
        }
        
        void ethercat_process_run(void *arg)
        {
            RTIME now, previous=0;
            int32_t toff=0;
            RTIME rt_ts = rt_timer_read();
        
            while (run)
            {
        ...bla bla...
        rtMessanger->somethingHappened();
            }
        }
        

        P.S.
        make sure your while (run) doesn't block the main event loop otherwise you'll probably need to move that part to a new thread

        S Offline
        S Offline
        spiderdab
        wrote on last edited by
        #3

        @VRonin Thank you for your quick answer.
        however I have an error on qDebug, saying: expected unqualified-id.
        I added #include <QDebug>, but I can't find around what I'm missing..

        1 Reply Last reply
        0
        • VRoninV Offline
          VRoninV Offline
          VRonin
          wrote on last edited by
          #4

          Does it matter? that's just placeholder code, don't waste your time on it. replace it with std::cout if need be

          "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
          ~Napoleon Bonaparte

          On a crusade to banish setIndexWidget() from the holy land of Qt

          S 1 Reply Last reply
          1
          • VRoninV VRonin

            Does it matter? that's just placeholder code, don't waste your time on it. replace it with std::cout if need be

            S Offline
            S Offline
            spiderdab
            wrote on last edited by
            #5

            @VRonin The class you suggested works ok.
            Thanks for your help.

            Pablo J. RoginaP 1 Reply Last reply
            2
            • S spiderdab

              @VRonin The class you suggested works ok.
              Thanks for your help.

              Pablo J. RoginaP Offline
              Pablo J. RoginaP Offline
              Pablo J. Rogina
              wrote on last edited by
              #6

              @spiderdab said in how can I create a signal to use from inside an rt_task?:

              The class you suggested works ok.

              great, so please don't forget to mark your post as solved!

              Upvote the answer(s) that helped you solve the issue
              Use "Topic Tools" button to mark your post as Solved
              Add screenshots via postimage.org
              Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

              1 Reply Last reply
              0

              • Login

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