Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. List: Pointers to different data type (objects)
Qt 6.11 is out! See what's new in the release blog

List: Pointers to different data type (objects)

Scheduled Pinned Locked Moved Solved General and Desktop
14 Posts 3 Posters 2.3k 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.
  • R Offline
    R Offline
    rbharadw
    wrote on last edited by
    #5

    Ok. Thank you. I am able to see the list of shapes with the different objects now. But the examples are still not clear how to access the individual methods and variables of these objects. I wish Qt provided more generic examples rather than just push buttons.

    Christian EhrlicherC JonBJ 2 Replies Last reply
    0
    • R rbharadw

      Ok. Thank you. I am able to see the list of shapes with the different objects now. But the examples are still not clear how to access the individual methods and variables of these objects. I wish Qt provided more generic examples rather than just push buttons.

      Christian EhrlicherC Online
      Christian EhrlicherC Online
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #6

      @rbharadw Read my link, learn c++

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      1 Reply Last reply
      4
      • R rbharadw

        Ok. Thank you. I am able to see the list of shapes with the different objects now. But the examples are still not clear how to access the individual methods and variables of these objects. I wish Qt provided more generic examples rather than just push buttons.

        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on last edited by JonB
        #7

        @rbharadw

        Shape *shape = arrayOfShapes[index];
        if (shape == nullptr)
            qDebug() << "nullptr";
        Cone *cone = dynamic_cast<Cone *>(shape);
        if (cone == nullptr)
            qDebug() << "It's not a Cone";
        else
        {
            qDebug() << "It is a Cone, so now I can go...";
            cone->coneMethod();
        }
        
        
        1 Reply Last reply
        4
        • R Offline
          R Offline
          rbharadw
          wrote on last edited by
          #8

          Thank you Jon and Christian.

          1 Reply Last reply
          0
          • R Offline
            R Offline
            rbharadw
            wrote on last edited by
            #9

            @JonB - The QObject class is quite handy. Is there a way I can use the indexOfClass or className() to recognize the subclass name (it can be cone or several other's..) in your loop? In other words, in your loop, where you define the Cone *cone = dynamic...., I would like to have a few lines to obtain the class name & then assign an object with this class name to do stuff. This will make it efficient because I might have a lot of such objects (not just cones or cylinders). Thanks for your reply as always.

            1 Reply Last reply
            0
            • R Offline
              R Offline
              rbharadw
              wrote on last edited by
              #10

              For example this piece of code gives me an index of -1 even though the Cone class name is correct. ```
              Shape *assem0 = new Shape("Assembly",0,0);

               Shape *Comp0 = new Cone;
               Cone *Cone1 = qobject_cast<Cone *>(Comp0);
              
               Shape *Comp1 = new Cylinder ;
               Cylinder *Cylinder1 = qobject_cast<rectConeComponent *>(Comp1);
              

              assem0->compGeomList.append(Cone1);
              assem0->compGeomList.append(Cylinder1);

              const char *name = assem0->constData()->metaObject()->className();
              int index = assem0->constData()->metaObject()->indexOfClassInfo(name);

              (note that the Shape class has a QVector<Shape> *compGeomList)

              JonBJ Christian EhrlicherC 2 Replies Last reply
              0
              • R rbharadw

                For example this piece of code gives me an index of -1 even though the Cone class name is correct. ```
                Shape *assem0 = new Shape("Assembly",0,0);

                 Shape *Comp0 = new Cone;
                 Cone *Cone1 = qobject_cast<Cone *>(Comp0);
                
                 Shape *Comp1 = new Cylinder ;
                 Cylinder *Cylinder1 = qobject_cast<rectConeComponent *>(Comp1);
                

                assem0->compGeomList.append(Cone1);
                assem0->compGeomList.append(Cylinder1);

                const char *name = assem0->constData()->metaObject()->className();
                int index = assem0->constData()->metaObject()->indexOfClassInfo(name);

                (note that the Shape class has a QVector<Shape> *compGeomList)

                JonBJ Offline
                JonBJ Offline
                JonB
                wrote on last edited by JonB
                #11

                @rbharadw
                Even if you can get at the class names, I wouldn't be going down that route.

                I would like to have a few lines to obtain the class name & then assign an object with this class name to do stuff

                This sounds fishy, whatever you might mean by it,.

                C++ is a typed language. Dynamic/QObject casting is the better way to go.

                i would wonder: why do you have a collection of these shapes and what you want to do is call subclass-specific methods on them? Is there a case for inherited methods from the base class, or intermediate classes for groups of common shapes which offer common methods? C++ also has template classes which could help, but still it's about finding common functionality.

                1 Reply Last reply
                0
                • R Offline
                  R Offline
                  rbharadw
                  wrote on last edited by
                  #12

                  @JonB What I meant was if there was a better than a check using a statement like this:
                  const char *name = assem0->constData()->metaObject()->className();
                  if (strcmp(name, "Cone")) qDebug() << "Name found, declare your cone class and do stuff";

                  Your question is very appropriate. I thought about templates, but the thing is that although the data type (like vertices etc) is same for these shapes there are some analysis that are different for each. I am not sure as yet but perhaps I should give this more thought & see if can just override these methods in the subclasses. Thanks for all the pointers, appreciate it.

                  1 Reply Last reply
                  0
                  • R rbharadw

                    For example this piece of code gives me an index of -1 even though the Cone class name is correct. ```
                    Shape *assem0 = new Shape("Assembly",0,0);

                     Shape *Comp0 = new Cone;
                     Cone *Cone1 = qobject_cast<Cone *>(Comp0);
                    
                     Shape *Comp1 = new Cylinder ;
                     Cylinder *Cylinder1 = qobject_cast<rectConeComponent *>(Comp1);
                    

                    assem0->compGeomList.append(Cone1);
                    assem0->compGeomList.append(Cylinder1);

                    const char *name = assem0->constData()->metaObject()->className();
                    int index = assem0->constData()->metaObject()->indexOfClassInfo(name);

                    (note that the Shape class has a QVector<Shape> *compGeomList)

                    Christian EhrlicherC Online
                    Christian EhrlicherC Online
                    Christian Ehrlicher
                    Lifetime Qt Champion
                    wrote on last edited by
                    #13

                    @rbharadw said in List: Pointers to different data type (objects):

                    For example this piece of code gives me an index of -1 even though the Cone class name is correct. ```
                    Shape *assem0 = new Shape("Assembly",0,0);
                    Cone *Cone1 = qobject_cast<Cone *>(Comp0);

                    When you use qobject_cast<> then you should read what's needed to make it work:
                    "... and be declared with the Q_OBJECT macro."

                    Make sure to know about he stuff you're using instead simply put something together and hope that it works somehow.

                    Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                    Visit the Qt Academy at https://academy.qt.io/catalog

                    1 Reply Last reply
                    1
                    • R Offline
                      R Offline
                      rbharadw
                      wrote on last edited by
                      #14

                      @Christian-Ehrlicher said in List: Pointers to different data type (objects):

                      Q_OBJEC

                      I already did this Christian. I appreciate you adhere to being a bit more polite in your replies rather than getting frustrated or replying as if the guy asking a question has no sense. I have been polite to you so far, but your tone of answers are not appreciated. If you cannot indulge in a professional and polite discussion, please abstain from giving replies. Please check other replies from Jon B, for example, very constructive and kind.

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