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. Can't access child method from parent?
Forum Updated to NodeBB v4.3 + New Features

Can't access child method from parent?

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 3 Posters 371 Views 1 Watching
  • 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.
  • C Offline
    C Offline
    Calicoder
    wrote on last edited by
    #1

    Good day, wondering if anyone has a clue what is happening here. I went through a C++ and QT tutorial and this should work I'm thinking. For some reason I can't see the ChildB getValues() method in the qDebug() << parent-> call in the runThis() method. Shouldn't that parameter QSharedPointer<Parent> parent have access to the child methods? I do get the getNames() method to show no issues

    void FunctionClass::runThis(QSharedPointer<Parent> parent)
    {
    	qDebug() << parent->???; // can only see the getNames() method, not the getValues()
    }
    
    class Parent : public QObject
    {
    	Parent(QObject *parent = nullptr);
    	virtual QString getNames() = 0;
    }
    
    class ChildA : public Parent
    {
    	QString getNames() override;
    }
    
    class ChildB : public Parent
    {
    	QString getNames() override;
    	int getValues();
    }
    
    M 1 Reply Last reply
    0
    • C Calicoder

      Good day, wondering if anyone has a clue what is happening here. I went through a C++ and QT tutorial and this should work I'm thinking. For some reason I can't see the ChildB getValues() method in the qDebug() << parent-> call in the runThis() method. Shouldn't that parameter QSharedPointer<Parent> parent have access to the child methods? I do get the getNames() method to show no issues

      void FunctionClass::runThis(QSharedPointer<Parent> parent)
      {
      	qDebug() << parent->???; // can only see the getNames() method, not the getValues()
      }
      
      class Parent : public QObject
      {
      	Parent(QObject *parent = nullptr);
      	virtual QString getNames() = 0;
      }
      
      class ChildA : public Parent
      {
      	QString getNames() override;
      }
      
      class ChildB : public Parent
      {
      	QString getNames() override;
      	int getValues();
      }
      
      M Offline
      M Offline
      mpergand
      wrote on last edited by mpergand
      #2

      @Calicoder
      Hi,

      parent knows nothing about ChildB.
      If parent is an instance of ChildB, you can do:

      ChildB* b=qobject_cast<ChildB*>(parent); 
      if(b) qDebug()<<b.getValues();
      

      [EDIT] or dynamic_cast (i see no Q_OBJECT declaration)

      C 1 Reply Last reply
      2
      • M mpergand

        @Calicoder
        Hi,

        parent knows nothing about ChildB.
        If parent is an instance of ChildB, you can do:

        ChildB* b=qobject_cast<ChildB*>(parent); 
        if(b) qDebug()<<b.getValues();
        

        [EDIT] or dynamic_cast (i see no Q_OBJECT declaration)

        C Offline
        C Offline
        Calicoder
        wrote on last edited by
        #3

        @mpergand Genius, need to read up on that qobject_cast code. This worked, thank you so much!

        jsulmJ 1 Reply Last reply
        0
        • C Calicoder

          @mpergand Genius, need to read up on that qobject_cast code. This worked, thank you so much!

          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @Calicoder You also need to understand why you need to cast (you can also use dynamic_cast).
          Parent has no getValues() method, so you need to cast the pointer to ChildB to be able to call this method. And of course you need to check the pointer after casting as parent is not going to always be ChildB instance.

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          2

          • Login

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