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



  • 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.


  • Lifetime Qt Champion

    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.


  • Qt Champions 2016

    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.



  • 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


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.