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. Question about events (new Qt user here)
Forum Updated to NodeBB v4.3 + New Features

Question about events (new Qt user here)

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 5 Posters 1.7k Views 2 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.
  • F Offline
    F Offline
    firefly_pdp
    wrote on last edited by
    #1

    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.

    Paul ColbyP aha_1980A JKSHJ 3 Replies Last reply
    0
    • F firefly_pdp

      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.

      Paul ColbyP Offline
      Paul ColbyP Offline
      Paul Colby
      wrote on last edited by Paul Colby
      #2

      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.

      1 Reply Last reply
      4
      • F firefly_pdp

        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.

        aha_1980A Offline
        aha_1980A Offline
        aha_1980
        Lifetime Qt Champion
        wrote on last edited by
        #3

        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!

        Qt has to stay free or it will die.

        1 Reply Last reply
        3
        • F Offline
          F Offline
          firefly_pdp
          wrote on last edited by
          #4

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

          1 Reply Last reply
          1
          • JonBJ Offline
            JonBJ Offline
            JonB
            wrote on last edited by
            #5

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

            1 Reply Last reply
            0
            • F firefly_pdp

              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.

              JKSHJ Offline
              JKSHJ Offline
              JKSH
              Moderators
              wrote on last edited by JKSH
              #6

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

              Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

              1 Reply Last reply
              2

              • Login

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