Qt Forum

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

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

    General and Desktop
    slot connection c++11
    3
    4
    2080
    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.
    • K
      ketjow last edited by ketjow

      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 Reply Quote 0
      • SGaist
        SGaist Lifetime Qt Champion last edited by

        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 Reply Quote 0
        • kshegunov
          kshegunov Moderators last edited by kshegunov

          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 Reply Quote 3
          • K
            ketjow last edited by ketjow

            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 Reply Quote 0
            • First post
              Last post