Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Run slots in order with function in between



  • 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!


  • Qt Champions 2019

    @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); });
    

  • Lifetime Qt Champion

    Hi,

    Where exactly is that code run in your application ?



  • 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.


  • Lifetime Qt Champion

    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.



  • 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?


  • Qt Champions 2019

    @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); });
    


  • 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.


  • Banned

    This post is deleted!

Log in to reply