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. Connect to overridden virtual slot
Forum Updated to NodeBB v4.3 + New Features

Connect to overridden virtual slot

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 3 Posters 1.6k Views 3 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.
  • M Offline
    M Offline
    mix359
    wrote on last edited by JKSH
    #1

    [EDIT: Changed title from "overloaded" -> "overridden" --JKSH]

    Hi to all,

    I'm facing some issue with a overloaded slot connected in the parent object with the new Qt syntax.
    I've done something like this

    class Base : public QObject {
        Q_OBJECT
    
    public:
        Base(QObject *p = nullptr) : QObject(p)
        {
            connect(senderObj, &SenderObj::signal, this, &Base::updateSize);
        }
    
        void virtual updateSize() {
            //do something
        }
    }
    
    class Derived : Base {
    
        Derived(QObject *p = nullptr) : QObject(p) {}
    
        void updateSize() {
            //do something other
        }
    }
    

    If I use the Base object or a derived object that doesn't override the updateSize function, all work as expected, but if I use the Derived object, when the signal is emitted, the application crash.
    I've looked a bit with the debugger and it seam that the error is caused by the meta system that try to use the wrong reference to the function (the one of the Base object), and I can understand that (effectively I'm registering the slot as &Base::updateSize), but I can't find I way to make that declaration "dynamic" and use the Derived object method instead of the Base member method.

    Any way of doing this? Or any workaround suggested for this situation?

    Thanks
    Cheers Mix

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

      Hi,

      Please provide a complete minimal compilable example showing the behavior you have.

      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
      2
      • Kent-DorfmanK Offline
        Kent-DorfmanK Offline
        Kent-Dorfman
        wrote on last edited by
        #3

        The meta system doesn't like combining of class declarations and implementations in a single file (a Qt design flaw, imho). Create your class declaration in a .h file and your implementation in a .cc (or chatever) file. Everything I've seen over the years seems to indicate that for Q_OBJECT classes you must separate the declaration and implementation into discrete files or the MOC breaks.

        1 Reply Last reply
        0
        • M Offline
          M Offline
          mix359
          wrote on last edited by
          #4

          Thanks for the replies :)

          That was just an example, the original code is already divided into the declaration and implementation files.

          Meanwhile I've probably found what's wrong: originally this method was a simple protected method and not a slot protected method.
          During my test to make it work, I've tried changing it to slot and use the old syntax, but I forgot to change the declaration in the Base class header file :(
          And searching information on the net have confused me, because there's many post about problems with overriden signal.

          So, after a qmake rebuild it's now working as expected...
          Shame on me... maybe that could be useful to someone with a similar problem: check twice the declaration!

          Thanks
          Cheers Mix

          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