Question about events (new Qt user here)



  • Hey all, I'm working on a personal project to try and learn Qt and had a question regarding events.

    My project has multiple UI forms, and the main window has a "main widget" that controls which "child" form to show. On one of these "child" forms, I have some line edit widgets. I want some kind of event/slot handler in the main window that triggers when the line edit on the "child" form finishes editing, but I'm not quite sure how to set that up. I know that you can set up the "editingFinished" slot for the QLineEdit, but that will create a handler in my "child" widget, and I want to handle this in the main window, which doesn't know the line edit exists.

    The way I was thinking you could do this is broadcast a custom event that the main window registers and listens for, but I couldn't find any examples of how to set that up, and I'm not even sure if that's the correct approach I should be taking.



  • Hi @firefly_pdp,

    The way I was thinking you could do this is broadcast a custom event that the main window registers and listens for, but I couldn't find any examples of how to set that up,

    Yeah, that's one way, and probably the best way for your use case.

    Assuming your "child forms" are widgets (or more specifically, QObject derived) then the forms can re-broadcast the 'editingFinished' event (or any other) like:

    class MyForm : public QWidget {
        Q_OBJECT
    
        ...
    
        protected slots:
            void onEditingFinished() {
                /// do stuff here. eg check that sender() == myFooLineEdit
                emit myFooEditingFinished(myFooLineEdit->text());
            }
    
        signals:
            void myFooEditingFinished(const QString &text);
    }
    

    Then in the main widget, something like:

        connect(form1, &MyForm::myFooEditingFinished, this, &MyMainWidget::onFooEditingFinished);
        connect(form2, ... );
        connect(form3, ... );
    

    Cheers.



  • Hi @firefly_pdp,

    the example from @Paul-Colby is quite good. you can improve it a bit as you don't need the slot between two signals, you can simply connect one signal to another one.

    Happy New Year!



  • Ah emit! That's what I was looking for. Thanks!



  • That emit "syntax" is so confusing.... IMHO anyway.


  • Moderators

    @firefly_pdp said in Question about events (new Qt user here):

    Hey all, I'm working on a personal project to try and learn Qt and had a question regarding events.

    Hi @firefly_pdp, it looks like your main question has been answered.

    I'd like to also highlight that there's a difference between "events" and "signals" in Qt:

    • Events are subclasses of QEvent: http://doc.qt.io/qt-5/qevent.html. These are handled by reimplementing QObject::event() (or similar convenience functions like QWidget::mouseEvent())
    • Signals are simply function signatures. These are handled by connecting the signal to a slot.

    Signals are far easier and nicer to use than QEvent. I have implemented tons of custom signals, but I can't remember the last time I implemented a custom QEvent.

    @JonB said in Question about events (new Qt user here):

    That emit "syntax" is so confusing.... IMHO anyway.

    I concur that it definitely doesn't look like "typical" C++. I'm used to it now though, and I just treat it as documentation to indicate that the code is emitting a signal, not calling a function. To me, emit foo() also feels similar to return bar().

    "emit" is a macro that expands to nothing, so omitting emit will make no difference at all to the compiled program.


Log in to reply
 

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