Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Special Interest Groups
  3. C++ Gurus
  4. SIGNAL/SLOT() macros in Qt's source code
QtWS25 Last Chance

SIGNAL/SLOT() macros in Qt's source code

Scheduled Pinned Locked Moved Solved C++ Gurus
19 Posts 8 Posters 1.5k Views
  • 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.
  • Axel SpoerlA Offline
    Axel SpoerlA Offline
    Axel Spoerl
    Moderators
    wrote on last edited by Axel Spoerl
    #2

    While it looks like something easy to change, disconnection during destruction is where it becomes tricky.
    SIGNAL/SLOT based connections get disconnected, when either of the owning classes gets destroyed.
    PMF connections stay alive, until the QObject is eventually destroyed.
    That can lead to crashes, if e.g. a signal is connected to &QLineEdit::setText and the line edit has become a plain QObject in the course of its destruction. When the signal is fired at this very moment, a dangling PMF would be called in a PMF based connection.
    A macro-based connection would have been gone already, so the signal would be dancing on its own.

    That said, I have learned my lesson fixing old style connections as a drive-by. You need to wrap your head around it and make sure they either don't cause any grief - or get disconnected at the right time.

    Software Engineer
    The Qt Company, Oslo

    J.HilkJ JonBJ S 3 Replies Last reply
    1
    • Axel SpoerlA Axel Spoerl

      While it looks like something easy to change, disconnection during destruction is where it becomes tricky.
      SIGNAL/SLOT based connections get disconnected, when either of the owning classes gets destroyed.
      PMF connections stay alive, until the QObject is eventually destroyed.
      That can lead to crashes, if e.g. a signal is connected to &QLineEdit::setText and the line edit has become a plain QObject in the course of its destruction. When the signal is fired at this very moment, a dangling PMF would be called in a PMF based connection.
      A macro-based connection would have been gone already, so the signal would be dancing on its own.

      That said, I have learned my lesson fixing old style connections as a drive-by. You need to wrap your head around it and make sure they either don't cause any grief - or get disconnected at the right time.

      J.HilkJ Offline
      J.HilkJ Offline
      J.Hilk
      Moderators
      wrote on last edited by
      #3

      @Axel-Spoerl and here I thought it was because no-one touched that class since 2016... 🙈


      Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


      Q: What's that?
      A: It's blue light.
      Q: What does it do?
      A: It turns blue.

      1 Reply Last reply
      0
      • Axel SpoerlA Axel Spoerl

        While it looks like something easy to change, disconnection during destruction is where it becomes tricky.
        SIGNAL/SLOT based connections get disconnected, when either of the owning classes gets destroyed.
        PMF connections stay alive, until the QObject is eventually destroyed.
        That can lead to crashes, if e.g. a signal is connected to &QLineEdit::setText and the line edit has become a plain QObject in the course of its destruction. When the signal is fired at this very moment, a dangling PMF would be called in a PMF based connection.
        A macro-based connection would have been gone already, so the signal would be dancing on its own.

        That said, I have learned my lesson fixing old style connections as a drive-by. You need to wrap your head around it and make sure they either don't cause any grief - or get disconnected at the right time.

        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on last edited by
        #4

        @Axel-Spoerl said in SIGNAL/SLOT() macros in Qt's source code:

        disconnection during destruction is where it becomes tricky

        Fair enough. But if Qt cannot get this correct, whatever it takes, in their own supplied code and so stick with old-style, what hope for us amateurs when we do use PMF in our own stuff?!

        1 Reply Last reply
        0
        • Christian EhrlicherC Offline
          Christian EhrlicherC Offline
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on last edited by
          #5

          Qt can do it correct but there are a lot of corner cases inside Qt which are hard to find out and there is a lack of manpower to change it. Also there is no reason to change it as it works.

          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
          Visit the Qt Academy at https://academy.qt.io/catalog

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

            Fair enough to @Axel-Spoerl & @Christian-Ehrlicher's answers.

            One minor irritation for me, as an online code browser (don't know if this also arises if you have source inside your IDE code model, but I suspect it does). I was trying to find where all the calls are in Qt source to QTableView::columnCountChanged(). Since it's a protected slot I know Qt source must call it. But the "helper" in code browser which allows you to see/go to all calls to a function, which I use a lot, was showing it only had 1 definition in .cpp and 1 declaration in .h, i.e. not used anywhere. I had to do a bit of searching to discover https://codebrowser.dev/qt6/qtbase/src/widgets/itemviews/qtableview.cpp.html#1371

            connect(d->horizontalHeader, SIGNAL(sectionCountChanged(int,int)),
                    this, SLOT(columnCountChanged(int,int)));
            

            since that does not show up as a call to sectionCountChanged() because of the macro-to-string, where a PMF connection would have allowed the code browser to spot it.

            1 Reply Last reply
            0
            • Axel SpoerlA Offline
              Axel SpoerlA Offline
              Axel Spoerl
              Moderators
              wrote on last edited by
              #7

              I fully agree with you, that macro based connections should all be removed.
              @Christian-Ehrlicher has submitted cleanups, and should you find it tempting to submit patches - we'd be grateful.
              I also clean them up whenever I find the time for it. Unfortunately there are other priorities, too. For all of us.

              Software Engineer
              The Qt Company, Oslo

              1 Reply Last reply
              0
              • GrecKoG Offline
                GrecKoG Offline
                GrecKo
                Qt Champions 2018
                wrote on last edited by
                #8

                Note that SIGNAL/SLOT syntax can also be used to add virtual dispatch to a function while not changing the binary compatibility of a class.
                Like https://doc.qt.io/qt-5/qnetworkaccessmanager.html#supportedSchemesImplementation that was added as a protected slot in Qt 5.2 because adding a virtual method was not possible until Qt 6.

                JonBJ 1 Reply Last reply
                4
                • GrecKoG GrecKo

                  Note that SIGNAL/SLOT syntax can also be used to add virtual dispatch to a function while not changing the binary compatibility of a class.
                  Like https://doc.qt.io/qt-5/qnetworkaccessmanager.html#supportedSchemesImplementation that was added as a protected slot in Qt 5.2 because adding a virtual method was not possible until Qt 6.

                  JonBJ Offline
                  JonBJ Offline
                  JonB
                  wrote on last edited by
                  #9

                  @GrecKo
                  Yes, I knew there were some "advanced" reasons/cases where (apparently) one has to use old-style. I just wondered whether there was any good reason why, say, the example I came across needed to use it. It seems not.

                  Pl45m4P 1 Reply Last reply
                  0
                  • Christian EhrlicherC Offline
                    Christian EhrlicherC Offline
                    Christian Ehrlicher
                    Lifetime Qt Champion
                    wrote on last edited by
                    #10

                    If you see the old signal/slot syntax in official Qt examples let us know so we can remove them - they should no longer be advertised.

                    Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                    Visit the Qt Academy at https://academy.qt.io/catalog

                    SGaistS 1 Reply Last reply
                    0
                    • JonBJ JonB has marked this topic as solved on
                    • Christian EhrlicherC Christian Ehrlicher

                      If you see the old signal/slot syntax in official Qt examples let us know so we can remove them - they should no longer be advertised.

                      SGaistS Offline
                      SGaistS Offline
                      SGaist
                      Lifetime Qt Champion
                      wrote on last edited by
                      #11

                      @Christian-Ehrlicher AFAIK, I took care of that except in some places where it was really not possible.

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

                      1 Reply Last reply
                      1
                      • JonBJ JonB

                        @GrecKo
                        Yes, I knew there were some "advanced" reasons/cases where (apparently) one has to use old-style. I just wondered whether there was any good reason why, say, the example I came across needed to use it. It seems not.

                        Pl45m4P Online
                        Pl45m4P Online
                        Pl45m4
                        wrote on last edited by
                        #12

                        Hi @JonB ,

                        I was also asking this myself when I was browsing the Qt source for my topic here.
                        There you can also find interesting mentions made by @Christian-Ehrlicher and @SGaist .

                        For example, I learned that the string-based and the PMF/Functor-based syntax are interchangeable in most cases, but not everywhere, as it is a widely spread myth :)
                        With both ways to connect you can do things the other way does not support.


                        If debugging is the process of removing software bugs, then programming must be the process of putting them in.

                        ~E. W. Dijkstra

                        1 Reply Last reply
                        0
                        • Axel SpoerlA Axel Spoerl

                          While it looks like something easy to change, disconnection during destruction is where it becomes tricky.
                          SIGNAL/SLOT based connections get disconnected, when either of the owning classes gets destroyed.
                          PMF connections stay alive, until the QObject is eventually destroyed.
                          That can lead to crashes, if e.g. a signal is connected to &QLineEdit::setText and the line edit has become a plain QObject in the course of its destruction. When the signal is fired at this very moment, a dangling PMF would be called in a PMF based connection.
                          A macro-based connection would have been gone already, so the signal would be dancing on its own.

                          That said, I have learned my lesson fixing old style connections as a drive-by. You need to wrap your head around it and make sure they either don't cause any grief - or get disconnected at the right time.

                          S Offline
                          S Offline
                          SimonSchroeder
                          wrote on last edited by
                          #13

                          @Axel-Spoerl said in SIGNAL/SLOT() macros in Qt's source code:

                          While it looks like something easy to change, disconnection during destruction is where it becomes tricky.
                          SIGNAL/SLOT based connections get disconnected, when either of the owning classes gets destroyed.
                          PMF connections stay alive, until the QObject is eventually destroyed.
                          That can lead to crashes, if e.g. a signal is connected to &QLineEdit::setText and the line edit has become a plain QObject in the course of its destruction. When the signal is fired at this very moment, a dangling PMF would be called in a PMF based connection.
                          A macro-based connection would have been gone already, so the signal would be dancing on its own.

                          I don't fully understand what you are saying here. I always thought that as long as you have a context object PMF and SIGNAL/SLOT would behave the same. Sure, lambdas cannot be disconnected automatically without a context object.

                          Where exactly is the problem? Does only SLOT enter the connection into a list of connected slots for the receiver and the PMF syntax does not do this? Is there a reason for this decision? Still, there must be some information saved to inform all potential senders at the point when the QObject base type is destructed.

                          @Pl45m4 said in SIGNAL/SLOT() macros in Qt's source code:

                          With both ways to connect you can do things the other way does not support.

                          Can you point to some differences? I only can think about new things the PMF syntax supports, like connecting to functions not declared as slots (i.e. even member functions of class not derived from QObject) and connecting to lambdas. What are things you can to with the string-based syntax that is impossible with PMF?

                          Pl45m4P 1 Reply Last reply
                          1
                          • Axel SpoerlA Offline
                            Axel SpoerlA Offline
                            Axel Spoerl
                            Moderators
                            wrote on last edited by
                            #14

                            IMHO, as a general rule, it's safe to use PMF syntax in applications.
                            The difference between "old" and "new" style kicks in when cascaded destruction takes place, and a signal is delivered to a stale object. In the Qt library, this happens (amongst others) to slots in private headers.

                            You can have a look at https://codereview.qt-project.org/c/qt/qtbase/+/507788 for instance.
                            Just replacing string based connections with PMF syntax lead to crashes, when the model of a combo box would still emit signals to the box's private header when it was already destroyed. QMetaObject deals with this automatically, because it knows to which class the connection belongs. A PMF connection doesn't know when the slot object's d'tor is reached, so it needs to be explicitly disconnected. Otherwise a signal could be delivered to QComboBoxPrivate when it has already degraded to a plain QObjectPrivate.

                            Software Engineer
                            The Qt Company, Oslo

                            JonBJ 1 Reply Last reply
                            1
                            • Axel SpoerlA Axel Spoerl

                              IMHO, as a general rule, it's safe to use PMF syntax in applications.
                              The difference between "old" and "new" style kicks in when cascaded destruction takes place, and a signal is delivered to a stale object. In the Qt library, this happens (amongst others) to slots in private headers.

                              You can have a look at https://codereview.qt-project.org/c/qt/qtbase/+/507788 for instance.
                              Just replacing string based connections with PMF syntax lead to crashes, when the model of a combo box would still emit signals to the box's private header when it was already destroyed. QMetaObject deals with this automatically, because it knows to which class the connection belongs. A PMF connection doesn't know when the slot object's d'tor is reached, so it needs to be explicitly disconnected. Otherwise a signal could be delivered to QComboBoxPrivate when it has already degraded to a plain QObjectPrivate.

                              JonBJ Offline
                              JonBJ Offline
                              JonB
                              wrote on last edited by
                              #15

                              @Axel-Spoerl said in SIGNAL/SLOT() macros in Qt's source code:

                              A PMF connection doesn't know when the slot object's d'tor is reached, so it needs to be explicitly disconnected.

                              Whaatt?! In user code (as opposed to whatever private inside Qt for its own purposes)?? I only use PMF, I have never had a problem, I am expecting it to deal with either signal or slot object being destroyed without my having to do anything. I suppose I may never have tested a slot object deletion, not sure...

                              1 Reply Last reply
                              0
                              • Axel SpoerlA Offline
                                Axel SpoerlA Offline
                                Axel Spoerl
                                Moderators
                                wrote on last edited by
                                #16

                                Whaatt?!

                                I guess you will never experience any issue with PMF based connections, unless you start plumbing with private classes created next to your public ones, go for multiple inheritance cascades and complex destruction with signals emitted to private headers.
                                I don't want to create any panic, I am just answering the question why we don't simply remove all string based connections in our code.

                                Software Engineer
                                The Qt Company, Oslo

                                1 Reply Last reply
                                2
                                • S SimonSchroeder

                                  @Axel-Spoerl said in SIGNAL/SLOT() macros in Qt's source code:

                                  While it looks like something easy to change, disconnection during destruction is where it becomes tricky.
                                  SIGNAL/SLOT based connections get disconnected, when either of the owning classes gets destroyed.
                                  PMF connections stay alive, until the QObject is eventually destroyed.
                                  That can lead to crashes, if e.g. a signal is connected to &QLineEdit::setText and the line edit has become a plain QObject in the course of its destruction. When the signal is fired at this very moment, a dangling PMF would be called in a PMF based connection.
                                  A macro-based connection would have been gone already, so the signal would be dancing on its own.

                                  I don't fully understand what you are saying here. I always thought that as long as you have a context object PMF and SIGNAL/SLOT would behave the same. Sure, lambdas cannot be disconnected automatically without a context object.

                                  Where exactly is the problem? Does only SLOT enter the connection into a list of connected slots for the receiver and the PMF syntax does not do this? Is there a reason for this decision? Still, there must be some information saved to inform all potential senders at the point when the QObject base type is destructed.

                                  @Pl45m4 said in SIGNAL/SLOT() macros in Qt's source code:

                                  With both ways to connect you can do things the other way does not support.

                                  Can you point to some differences? I only can think about new things the PMF syntax supports, like connecting to functions not declared as slots (i.e. even member functions of class not derived from QObject) and connecting to lambdas. What are things you can to with the string-based syntax that is impossible with PMF?

                                  Pl45m4P Online
                                  Pl45m4P Online
                                  Pl45m4
                                  wrote on last edited by Pl45m4
                                  #17

                                  @SimonSchroeder said in SIGNAL/SLOT() macros in Qt's source code:

                                  What are things you can to with the string-based syntax that is impossible with PMF?

                                  What @Christian-Ehrlicher said here

                                  • https://forum.qt.io/post/804827

                                  and here

                                  • https://forum.qt.io/post/804829

                                  That's (one of ) the reason(s) why the String-based syntax is still used with Qt's pImpl implementation and for "private slots".

                                  AFAIK the PMF can connect to non slot-declared functions, but only within QObject-derived classes.
                                  The string-based connection does not need QObject receiver.


                                  If debugging is the process of removing software bugs, then programming must be the process of putting them in.

                                  ~E. W. Dijkstra

                                  GrecKoG 1 Reply Last reply
                                  1
                                  • Pl45m4P Pl45m4

                                    @SimonSchroeder said in SIGNAL/SLOT() macros in Qt's source code:

                                    What are things you can to with the string-based syntax that is impossible with PMF?

                                    What @Christian-Ehrlicher said here

                                    • https://forum.qt.io/post/804827

                                    and here

                                    • https://forum.qt.io/post/804829

                                    That's (one of ) the reason(s) why the String-based syntax is still used with Qt's pImpl implementation and for "private slots".

                                    AFAIK the PMF can connect to non slot-declared functions, but only within QObject-derived classes.
                                    The string-based connection does not need QObject receiver.

                                    GrecKoG Offline
                                    GrecKoG Offline
                                    GrecKo
                                    Qt Champions 2018
                                    wrote on last edited by
                                    #18

                                    @Pl45m4 said in SIGNAL/SLOT() macros in Qt's source code:

                                    The string-based connection does not need QObject receiver.

                                    The signature is QObject::connect(const QObject *, const char *, const QObject *, const char *, Qt::ConnectionType ) though

                                    SGaistS 1 Reply Last reply
                                    0
                                    • GrecKoG GrecKo

                                      @Pl45m4 said in SIGNAL/SLOT() macros in Qt's source code:

                                      The string-based connection does not need QObject receiver.

                                      The signature is QObject::connect(const QObject *, const char *, const QObject *, const char *, Qt::ConnectionType ) though

                                      SGaistS Offline
                                      SGaistS Offline
                                      SGaist
                                      Lifetime Qt Champion
                                      wrote on last edited by
                                      #19

                                      @GrecKo said in SIGNAL/SLOT() macros in Qt's source code:

                                      @Pl45m4 said in SIGNAL/SLOT() macros in Qt's source code:

                                      The string-based connection does not need QObject receiver.

                                      The signature is QObject::connect(const QObject *, const char *, const QObject *, const char *, Qt::ConnectionType ) though

                                      This was referring to this overload:

                                      QMetaObject::Connection QObject::connect(const QObject *sender, const char *signal, const char *method, Qt::ConnectionType type = Qt::AutoConnection) const
                                      

                                      Which implies that the target is the current object.

                                      This is different from the similar (yet different) PMF variant as there's no implicit target in that case. Hence the recommendation to use the variant which has a context object.

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

                                      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