Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Forum Updated on Feb 6th

    [Solved] Slot inheritance problem despite Q_Object Macro

    General and Desktop
    4
    6
    1513
    Loading More Posts
    • 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
      fant last edited by

      Hello,

      Currently I have a strange problem with slot inheritance. In other forums this problem will talked, too, but it doesn't help me.

      At first, I have the abstract superclass (non-essential things I removed):

      @class ToyObject {

          Q_OBJECT
      
          Q_PROPERTY(QString _name READ getName WRITE setName)
      
      public:
          explicit ToyObject(QObject *parent = 0);
      
      protected:
          QString _name;
      
      public slots:
          void setName(QString val) { _name = val; }
      
          //....
      

      };@

      Then my subclass (overwrites the abstract virtual method, that not showed here):

      @class GCharacter : public ToyObject {

          Q_OBJECT
      
      public:
          explicit GCharacter(QObject *parent = 0);
      
          //....
      

      };@

      No I used the polymorphism in an other class called OInspector. In header file I declared following methods:

      @
      void defaultPreparation(ToyObject *to);

      void prepare(GCharacter *gCharacter);
      @

      And implements this:

      @
      void OInspector::prepare(GCharacter *gCharacter){

      defaultPreparation(gCharacter);
      
      //connection to another Slot of GCharacter, that works
      QSpinBox *spinFram = createSpinner();
      connect(spinFram, SIGNAL(valueChanged(int)), gCharacter, SLOT(setNumOfFrames(int)));
      

      }

      void OInspector::defaultPreparation(ToyObject *to){

      _nameEdit = new QLineEdit(to->getName(), this);
      connect(_nameEdit, SIGNAL(textChanged(QString)), to, SLOT(setName(QString)));
      

      }
      @

      When I run the application, I get the following warning code:

      bq. QObject::connect: No such slot GCharacter::setName(QString) in ...

      But GCharacter should inherit the slot from ToyObject, or not? In QtCreator I can call the inherited method without problems.

      Thanks for help.

      1 Reply Last reply Reply Quote 0
      • JKSH
        JKSH Moderators last edited by

        Hi,

        [quote]@class ToyObject {
        ...@
        ...
        GCharacter should inherit the slot from ToyObject, or not?[/quote]Yes, it should inherit the slot from ToyObject, if you do the following:

        Make sure ToyObject publicly inherits QObject

        Make sure you declare both ToyObject and GCharacter in a .h file, not a .cpp file

        Run qmake

        Recompile your project

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

        1 Reply Last reply Reply Quote 0
        • F
          fant last edited by

          1. Yes it does. ToyObject is a subclass of ISerializable custom class, which inherit from QObject.

          2. Yes, both are in a headerfile declared.

          3.+4. Done. But nothing changes.

          1 Reply Last reply Reply Quote 0
          • F
            frankiefrank last edited by

            I just wanted to iterate what JSKH wrote, is this public inheritance? i.e. your class headers look like

            @
            class ToyObject : public ISerializable
            @

            and

            @
            class ISerializable : public QObject
            @
            ?

            "Roads? Where we're going, we don't need roads."

            1 Reply Last reply Reply Quote 0
            • hskoglund
              hskoglund last edited by

              Hmm interesting! Just guessing:
              perhaps the class inheritance stuff works better if you switch to Qt5-flavored connects, i.e. instead of:
              @connect(_nameEdit, SIGNAL(textChanged(QString)), to, SLOT(setName(QString)));@

              you try
              @connect(_nameEdit, &QLineEdit::textChanged, to, &ToyObject::setName);@

              1 Reply Last reply Reply Quote 0
              • F
                fant last edited by

                Yes, it is public inheritance, too. In any case.. checked.

                hskoglund: You are a genius! It works! :)

                So my conclusion:
                If you want to work with inherited slots in Qt 5, use the new signal-slot-syntax. Apart from that, I like more the old syntax - the reason follows:
                If I connect for example a QComboBox with another object and want to use the signal currentIndexChanged, there are two possible methods (with Integer or QString argument). So the compiler has a problem to decide, which signal is wanted. I have to explicitly tell the compiler the correct functions address:

                @
                connect(combobox,
                static_cast<void (QComboBox::*)(int)>
                (&QComboBox::currentIndexChanged),
                object,
                &MyObjectClass::workWithData);
                @

                Here some other references for people who have a similar problem:
                "New Signal Slot Syntax":http://qt-project.org/wiki/New_Signal_Slot_Syntax
                "New Signal Slot Syntax with QComboBox":http://qt-project.org/forums/viewthread/21513

                Thank you all for help. :)

                1 Reply Last reply Reply Quote 0
                • First post
                  Last post