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. Run slots in order with function in between
Forum Updated to NodeBB v4.3 + New Features

Run slots in order with function in between

Scheduled Pinned Locked Moved Solved General and Desktop
8 Posts 4 Posters 563 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.
  • T Offline
    T Offline
    Tiago M.Pinto
    wrote on last edited by
    #1

    Hi!

    I am trying to emit a signal (which should trigger its slot immediately) at the beginning of a function, then I wait for another function to return and then I emit another signal whose slot should be the last thing to run. Here is the example:

    void mainFun(void)
    {
       emit signal1();
    
       subFun();
    
       emit signal2();
    }
    

    I already tried different types of connections (Qt::DirectConnection and Qt::QueuedConnection) but both slots seem to run at the end of mainFun() after subFun() has returned.

    Is there any way I could solve this?

    Thanks!

    1 Reply Last reply
    0
    • T Tiago M.Pinto

      Thank you @SGaist

      I tried to implement a simple machine consisting in switching two buttons on and off but it is not working. This is the code in my object's constructor:

      m_machine = new QStateMachine(this);
      QState *state1 = new QState();
      QState *state2 = new QState();
      
      state1->addTransition(button1,&QPushButton::clicked,state2);
      state2->addTransition(button2,&QPushButton::clicked,state1);
      
      QObject::connect(state1,&QState::entered,button2,&QPushtButton::setDisabled);
      QObject::connect(state1,&QState::entered,button1,&QPushtButton::setEnabled);
      QObject::connect(state2,&QState::entered,button2,&QPushtButton::setEnabled);
      QObject::connect(state2,&QState::entered,button1,&QPushtButton::setDisabled);
      
      m_machine->addState(state1);
      m_machine->addState(state2);
      m_machine->setInitialState(state1);
      m_machine->start();
      

      I know that the slot QPushtButton::setDisabled expects a boolean and the signal QState::entered is not providing it, but I didn't figure out yet how to enable/disable buttons using state machines. Is there another way?

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #6

      @Tiago-M-Pinto said in Run slots in order with function in between:

      is not providing it, but I didn't figure out yet how to enable/disable buttons using state machines

      You can use lambdas:

      QObject::connect(state1, &QState::entered, [this](){ button2->setEnabled(false); });
      

      https://forum.qt.io/topic/113070/qt-code-of-conduct

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

        Hi,

        Where exactly is that code run in your application ?

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

        T 1 Reply Last reply
        1
        • SGaistS SGaist

          Hi,

          Where exactly is that code run in your application ?

          T Offline
          T Offline
          Tiago M.Pinto
          wrote on last edited by
          #3

          Hi @SGaist,

          This mainFun() is actually another slot from an object and it is called after a signal is emitted when I click in a button of that object. What I want is to disable some buttons when I click in that button, then run subFun() and finally enable the buttons again.

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

            Sounds like you should handle these things whith a small QStateMachine. On entering the state, disable what you want and on leaving the state change stuff back again.

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

            T 1 Reply Last reply
            2
            • SGaistS SGaist

              Sounds like you should handle these things whith a small QStateMachine. On entering the state, disable what you want and on leaving the state change stuff back again.

              T Offline
              T Offline
              Tiago M.Pinto
              wrote on last edited by Tiago M.Pinto
              #5

              Thank you @SGaist

              I tried to implement a simple machine consisting in switching two buttons on and off but it is not working. This is the code in my object's constructor:

              m_machine = new QStateMachine(this);
              QState *state1 = new QState();
              QState *state2 = new QState();
              
              state1->addTransition(button1,&QPushButton::clicked,state2);
              state2->addTransition(button2,&QPushButton::clicked,state1);
              
              QObject::connect(state1,&QState::entered,button2,&QPushtButton::setDisabled);
              QObject::connect(state1,&QState::entered,button1,&QPushtButton::setEnabled);
              QObject::connect(state2,&QState::entered,button2,&QPushtButton::setEnabled);
              QObject::connect(state2,&QState::entered,button1,&QPushtButton::setDisabled);
              
              m_machine->addState(state1);
              m_machine->addState(state2);
              m_machine->setInitialState(state1);
              m_machine->start();
              

              I know that the slot QPushtButton::setDisabled expects a boolean and the signal QState::entered is not providing it, but I didn't figure out yet how to enable/disable buttons using state machines. Is there another way?

              jsulmJ 1 Reply Last reply
              0
              • T Tiago M.Pinto

                Thank you @SGaist

                I tried to implement a simple machine consisting in switching two buttons on and off but it is not working. This is the code in my object's constructor:

                m_machine = new QStateMachine(this);
                QState *state1 = new QState();
                QState *state2 = new QState();
                
                state1->addTransition(button1,&QPushButton::clicked,state2);
                state2->addTransition(button2,&QPushButton::clicked,state1);
                
                QObject::connect(state1,&QState::entered,button2,&QPushtButton::setDisabled);
                QObject::connect(state1,&QState::entered,button1,&QPushtButton::setEnabled);
                QObject::connect(state2,&QState::entered,button2,&QPushtButton::setEnabled);
                QObject::connect(state2,&QState::entered,button1,&QPushtButton::setDisabled);
                
                m_machine->addState(state1);
                m_machine->addState(state2);
                m_machine->setInitialState(state1);
                m_machine->start();
                

                I know that the slot QPushtButton::setDisabled expects a boolean and the signal QState::entered is not providing it, but I didn't figure out yet how to enable/disable buttons using state machines. Is there another way?

                jsulmJ Offline
                jsulmJ Offline
                jsulm
                Lifetime Qt Champion
                wrote on last edited by
                #6

                @Tiago-M-Pinto said in Run slots in order with function in between:

                is not providing it, but I didn't figure out yet how to enable/disable buttons using state machines

                You can use lambdas:

                QObject::connect(state1, &QState::entered, [this](){ button2->setEnabled(false); });
                

                https://forum.qt.io/topic/113070/qt-code-of-conduct

                T 1 Reply Last reply
                0
                • jsulmJ jsulm

                  @Tiago-M-Pinto said in Run slots in order with function in between:

                  is not providing it, but I didn't figure out yet how to enable/disable buttons using state machines

                  You can use lambdas:

                  QObject::connect(state1, &QState::entered, [this](){ button2->setEnabled(false); });
                  
                  T Offline
                  T Offline
                  Tiago M.Pinto
                  wrote on last edited by
                  #7

                  Hi @jsulm, thanks!

                  I didn't think of that. It works but I needed to capture my button2 to the lambda function instead of this pointer.
                  In the meanwhile I figured out how to solve this the other way. I found the signal &QState::activeChanged that emits the boolean as I wanted.

                  Thank you all. I will mark this topic as solved.

                  1 Reply Last reply
                  0
                  • G Offline
                    G Offline
                    gracieanderson
                    Banned
                    wrote on last edited by
                    #8
                    This post is deleted!
                    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