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. Connect slot of "wrong" (but derived) class
QtWS25 Last Chance

Connect slot of "wrong" (but derived) class

Scheduled Pinned Locked Moved Solved C++ Gurus
5 Posts 2 Posters 574 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.
  • JonBJ Offline
    JonBJ Offline
    JonB
    wrote on last edited by JonB
    #1

    Gurus' opinions wanted :)

    While messing about I deliberately tried this:

    class AWidget : public QWidget
    {
        void method()
        {
            connect(something, &Something::signal, this, &QPushButton::setEnabled);
        }
    }
    

    Note that it is class QWidget but the slot is QPushButton::setEnabled().

    This compiles (gcc, I think I am (default) C++11) without warning and works. Because, I guess, QPushButton does not have its own setEnabled() but inherits it directly from QWidget.

    At some future date, QPushButton::setEnabled() is given its own override. At which point (I presume) compilation will error and I'll have to change to &QWidget::setEnabled. Which is what I should have written in the first place.

    Should C++ compiler warn me about this in the first place, to avoid this? Discuss :)

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

      See it as an offsetOf() - it finds the offset of QPushButton::setEnabled and uses them - no more checks can be done here by the compiler.

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

      JonBJ 1 Reply Last reply
      4
      • Christian EhrlicherC Offline
        Christian EhrlicherC Offline
        Christian Ehrlicher
        Lifetime Qt Champion
        wrote on last edited by
        #2

        I don't see how a compiler could warn here. You have to pass a function pointer to an existing function what you're doing here.

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

        JonBJ 1 Reply Last reply
        1
        • Christian EhrlicherC Christian Ehrlicher

          I don't see how a compiler could warn here. You have to pass a function pointer to an existing function what you're doing here.

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

          @Christian-Ehrlicher

          connect(something, &Something::signal, this, &QPushButton::setEnabled);

          this is a QWidget. Writing &QPushButton::setEnabled, from my point of view at least, means I am looking for a method of a QPushButton. Which is not suitable for a QWidget!

          If I wrote &QPushButton::showMenu it would compile-error. If I could find some method of QWidget which QPushButton overrides, I believe that would error too. It only accepts &QPushButton::setEnabled because there is a &QWidget::setEnabled inherited unchanged by QPushButton.

          When this is a QWidget, I don't want to be allowed to specify a method via &QPushButton::anything. Only via &QWidget::something. Safe, right. Can't I have that?

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

            See it as an offsetOf() - it finds the offset of QPushButton::setEnabled and uses them - no more checks can be done here by the compiler.

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

            JonBJ 1 Reply Last reply
            4
            • Christian EhrlicherC Christian Ehrlicher

              See it as an offsetOf() - it finds the offset of QPushButton::setEnabled and uses them - no more checks can be done here by the compiler.

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

              @Christian-Ehrlicher
              Yep, thanks @Christian-Ehrlicher, at least I know where I am :)
              I'll mark yours as solution tomorrow, just in case anyone else finds this worthy of commenting in....

              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