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. Initialize child object to abstract class - not found slot present in child
Forum Update on Monday, May 27th 2025

Initialize child object to abstract class - not found slot present in child

Scheduled Pinned Locked Moved Solved General and Desktop
slotconnectionc++11
4 Posts 3 Posters 2.4k 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.
  • K Offline
    K Offline
    ketjow
    wrote on last edited by ketjow
    #1

    Hi,
    In my program I generate two kind of tables: pizza/drink, to not repeat myself I create AbstractItemsTableManager class, which have slot to implement:

    public slots:
       virtual void itemToAdd(AbstractFood* item)=0;
    

    then in class PizzaItemsTableManager which extends from AbstractItemsTableManager I have this kind of slots implemented:

    public slots:
        void itemToAdd(AbstractFood* item);
        void pizzaToCustomize(Pizza *pizza);
    

    In class, where I choose which table I want to create I use this kind of object AbstractItemsTableManager *abstractItemsTableManager; , becaouse all types of tables will be inherit from this class. And here is the problem, when I make initialization abstractItemsTableManager = new PizzaItemsTableManager(this->mainLayout); I get info

    QObject::connect: No such slot AbstractItemsTableManager::pizzaToCustomize(Pizza*) in ..\FoodClientApp\AvailableFoodTableManager\pizzaitemstablemanager.cpp:61
    

    This is method to which refers this warning

     void PizzaItemsTableManager::initializeDelegate()
    {
        pizzaDelegate = new PizzaListItemDelegate(layoutToFill);
    
        this->connect(pizzaDelegate,SIGNAL(itemToAdd(AbstractFood*)), this, SLOT(itemToAdd(AbstractFood*)), Qt::UniqueConnection );
        this->connect(pizzaDelegate,SIGNAL(passPizzaToCustomize(Pizza*)), static_cast<PizzaItemsTableManager*>(this), SLOT(pizzaToCustomize(Pizza*)), Qt::UniqueConnection);
    }
    

    this methiod is invoked in constructor of PizzaItemsTableManager class.

    The question is, how to resolve this error ? The problem may be connected with fact that AbstractItemsTableManager haven't got this slot.

    If this code isn't enough I can share whole project.

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

      Hi,

      Why are you doing that static_cast in the connect statement ?

      If you are using Qt 5, you should use the new syntax, that will give you compile time errors/warnings if something is not setup correctly.

      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
      • kshegunovK Offline
        kshegunovK Offline
        kshegunov
        Moderators
        wrote on last edited by kshegunov
        #3

        Hello,
        Signals and slots are just regular functions, so everything that is true for subclassing in general is true for them also. This means that your abstract class is not obligated to know any of the slots of its derived classes. Do make sure, though, that when you subclass QObject you put the Q_OBJECT macro on top of your class, this is needed to use signal-slots in Qt.
        Also @SGaist correctly notes that you have no need to cast your object before passing it to QObject::connect, they are all, in the end, sublcasses of QObject.

        As a side note:
        I saw several times in the forums here that people are not declaring their overrides as virtual, this is syntactically correct, but I would suggest doing it (makes the code more readable) or even better is to use C++11's override specifier.

        Read and abide by the Qt Code of Conduct

        1 Reply Last reply
        3
        • K Offline
          K Offline
          ketjow
          wrote on last edited by ketjow
          #4

          Thx so much @kshegunov , I had Q_OBJECT in abstract class, but I forgot to put it as well in derived class. This solve my problem :)

          
          As a side note:
          I saw several times in the forums here that people are not declaring their overrides as virtual, this is syntactically correct, but I would suggest doing it (makes the code more readable) or even better is to use C++11's override specifier.
          

          Thx for this tip so much, I like this kind of notes which helps me improve the code. Yeah you're ride it looks more readable right now :)

          Parent

          public slots:
             virtual void itemToAdd(AbstractFood* item)=0;
          

          Child:

          public slots:
              void itemToAdd(AbstractFood* item) override;
              void pizzaToCustomize(Pizza *pizza);
          

          and we exactly know what was overriden from parent.
          Many thx

          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