Signal order



  • Hi,
    There are two slots in my program:

    • void Dialog::on_energy_in_editingFinished()
    • void Dialog::on_button_clicked()

    Knowing that "button" is default, when "Enter" key is pressed while the focus is on the "energy" spinbox, both "editingFinished()" and "clicked()" signals are emitted.

    However, I need the first slot to always be executed before the second one, when "Enter" key is pressed. How can I make sure that the signals are emitted in the correct order?

    Thank you,
    Telergoel


  • Qt Champions 2016

    @Telergoel
    Hello,

    However, I need the first slot to always be executed before the second one, when "Enter" key is pressed. How can I make sure that the signals are emitted in the correct order?

    Why? You should not depend on the order (it's implementation defined), the same as with events - do not require a specific order.

    Kind regards.



  • Thanks for your answer!

    My point is that the second slot execution needs a value that is calculated in the first slot... That's why I need the signals to respect a specific order.

    If it's implementation-defined, I don't know what to type to make sure signals are sent in the order I need. When I press "Enter", which signal is emitted in the first place? "editingFinished()" or "clicked()"?

    What do you advise me to do?


  • Qt Champions 2016

    @Telergoel

    My point is that the second slot execution needs a value that is calculated in the first slot... That's why I need the signals to respect a specific order. ... What do you advise me to do?

    Then you need to signal from your own object. Think event driven - you wouldn't post a mouse event before the mouse has moved, right? So it's the same here. Qt's signal will trigger your slot, you do whatever calculations you need and raise your own signal - thus notifying any object that's interested in it. That signal can be connected to another slot and so on. E.g:

    class MyObject : public QObject
    {
        Q_OBJECT
    
    signals:
        void myDataReady(int);
    
    private slots:
        void prepareMyData();
    };
    
    void MyObject::prepareMyData()
    {
        // Do whatever calculation you need to do (this is connected to the clicked signal).
        int calculatedValue = 0;
    
        // When everything is ready, emit the appropriate signal
        emit myDataReady(calculatedValue);
    }
    

    So you connect things up:

    QPushButton button; //< Comes from the dialog 
    MyObject handler; //< This is the object handling the signal (QObject in this simple example, but can be anything).
    
    QObject::connect(&button, SIGNAL(clicked()), &handler, SLOT(prepareMyData()));
    QObject::connect(&handler, SIGNAL(myDataReady(int)), ...); //< Connect as needed
    

    If it's implementation-defined

    Slots are called in the order of the connection done to the signal when working in single thread and there's no guaranteed order when working in multiple thread environment. But this is an implementation detail and can change at any one time. As for the signals, I don't know which is emitted first I haven't checked the source.

    Kind regards.



  • Okay, I've got it now :)
    Thanks a lot for your help, I appreciate.
    Regards


  • Qt Champions 2016

    @Telergoel
    I'm glad I could help.
    Kind regards.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.