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. Issue with C++ inheritance working with Slots and Signals
Forum Updated to NodeBB v4.3 + New Features

Issue with C++ inheritance working with Slots and Signals

Scheduled Pinned Locked Moved General and Desktop
12 Posts 8 Posters 8.6k Views 1 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.
  • K Offline
    K Offline
    kloveridge
    wrote on last edited by
    #3

    If I make it virtual, that does work. My only problem with this approach would be I would have a bunch of virtual functions in the base class that would only be relevant to a specific inheritance.

    While this approach seems doable, it does seem like a hack.

    I'm connecting the signals and slots like this (this just a test case, so it has flaws:

    @ connect(m_fields[_index],
    SIGNAL(valueChanged(double)),
    this,
    SLOT(vchange(double)))@

    1 Reply Last reply
    0
    • K Offline
      K Offline
      kloveridge
      wrote on last edited by
      #4

      Ok, this seems to work. Rather than the base class being a QObject, just didn't have it inherit a QObject. It isn't necessary to inherit QObject because the base class is purely utilitarian and is common just so I can easily manage a bunch of these at the same time.

      But in my inherited class, I declared it like this:

      @class CboxXform : public QObject, CboxBase@

      Then the signals and slots are individualized to the specific class. This seems to work.

      1 Reply Last reply
      0
      • T Offline
        T Offline
        thiberi0
        wrote on last edited by
        #5

        "If I make it virtual, that does work. My only problem with this approach would be I would have a bunch of virtual functions in the base class that would only be relevant to a specific inheritance. "
        I'm not a expertise in C++, but I think you should look a design pattern to solve this problem, inheritance maybe cause a maintenance problem for you in future. I guess isolating the method that varies would be a better approach. Someone with more experience could point out witch patter could be used for your case...

        thiberi0

        1 Reply Last reply
        0
        • mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by
          #6

          Just a thought. Might be totally off :)
          Does your m_fields array have pointers to the base class ?
          So when you say connect this object , it thinks you mean the base object as
          there is no virtual function involved/anything that would have it resolve to the child. (CboxXform)
          if you tried (for test) to type case it to the correct class in the connect call?

          1 Reply Last reply
          0
          • F Offline
            F Offline
            flandi
            wrote on last edited by
            #7

            You should avoid multi inheritance from QObject otherwise you might get in unpredictable trouble...

            I guess your problem is that you missed the Q_OBJECT macro for CboxXform so the moc does not generate the needed meta glue code to find the declared slots.

            Or you could try to use the new connect syntax, that should work with every method even if it's not declared as a slot:

            @
            connect(signalObject,
            &SignalClassName::signalName,
            slotObject,
            &SlotClassName::slotName)
            @

            /* no comment */

            1 Reply Last reply
            0
            • K Offline
              K Offline
              kloveridge
              wrote on last edited by
              #8

              I will give these a try. I'm not a big fan of the multiple inheritance either.

              1 Reply Last reply
              0
              • TheBadgerT Offline
                TheBadgerT Offline
                TheBadger
                wrote on last edited by
                #9

                [quote author="flandi" date="1412760724"]
                I guess your problem is that you missed the Q_OBJECT macro for CboxXform so the moc does not generate the needed meta glue code to find the declared slots.
                [/quote]

                I think flandi is right, in your original code just add the Q_OBJECT macro to the CboxXform and it should work.


                Check out my SpellChecker Plugin for Qt Creator @ https://github.com/CJCombrink/SpellChecker-Plugin

                1 Reply Last reply
                0
                • SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #10

                  Hi,

                  [quote author="flandi" date="1412760724"]You should avoid multi inheritance from QObject otherwise you might get in unpredictable trouble...
                  [/quote]

                  On the contrary, it's predictable: you can't inherit QObject multiple times. "Here":http://doc.qt.digia.com/qq/qq15-academic.html#multipleinheritance you have an interesting article on subject

                  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
                  • G Offline
                    G Offline
                    giesbert
                    wrote on last edited by
                    #11

                    The inherointance in the beginning is correct, but the Q_OBJECT macro is missing in the derived class. Having that, you will get the moc class and it should work :-)

                    Nokia Certified Qt Specialist.
                    Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

                    1 Reply Last reply
                    0
                    • K Offline
                      K Offline
                      kloveridge
                      wrote on last edited by
                      #12

                      I ended up up ditching the base class design. So I avoided the issue altogether (for now).

                      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