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. [Solved] Slot inheritance problem despite Q_Object Macro
QtWS25 Last Chance

[Solved] Slot inheritance problem despite Q_Object Macro

Scheduled Pinned Locked Moved General and Desktop
6 Posts 4 Posters 1.8k 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.
  • F Offline
    F Offline
    fant
    wrote on last edited by
    #1

    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
    0
    • JKSHJ Offline
      JKSHJ Offline
      JKSH
      Moderators
      wrote on last edited by
      #2

      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
      0
      • F Offline
        F Offline
        fant
        wrote on last edited by
        #3
        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
        0
        • F Offline
          F Offline
          frankiefrank
          wrote on last edited by
          #4

          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
          0
          • hskoglundH Offline
            hskoglundH Offline
            hskoglund
            wrote on last edited by
            #5

            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
            0
            • F Offline
              F Offline
              fant
              wrote on last edited by
              #6

              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
              0

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved