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. If we have object name. Is their any way of getting the class to which it belongs dynamically?
Forum Updated to NodeBB v4.3 + New Features

If we have object name. Is their any way of getting the class to which it belongs dynamically?

Scheduled Pinned Locked Moved General and Desktop
12 Posts 4 Posters 16.2k 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.
  • P Offline
    P Offline
    pratik041
    wrote on last edited by
    #3

    [quote author="ddriver" date="1330085230"]IF you mean the QObject inherited parent, then YES, the address of the parent ... well, is the parent :)

    Also object instances CANNOT have names that begin with a number.[/quote]

    I mean that suppose i have object name can i find the object type before using
    @this->findChild<typecast*>("object name");@

    Pratik Agrawal

    1 Reply Last reply
    0
    • ? This user is from outside of this forum
      ? This user is from outside of this forum
      Guest
      wrote on last edited by
      #4

      ..ops sorry I misunderstood your inquiry

      1 Reply Last reply
      0
      • P Offline
        P Offline
        pratik041
        wrote on last edited by
        #5

        [quote author="ddriver" date="1330085956"]Acording to the DOC you can:

        @ QPushButton *button = parentWidget->findChild<QPushButton *>("button1");@

        [/quote]
        ya i have seen that but is their any way to know dynamically to which class type we should typecast(like here we have QPushbutton but it is specific i want something generic).
        Because i have multiple object of different type.

        Pratik Agrawal

        1 Reply Last reply
        0
        • L Offline
          L Offline
          lgeyer
          wrote on last edited by
          #6

          [quote author="pratik041" date="1330085607"]
          I mean that suppose i have object name can i find the object type before using.[/quote]
          No, because template functions are resolved at compile time, not runtime. You will have to use qobject_cast().

          @
          QObject object = findChild<QObject>("object name");

          QWidget widget = qobject_cast<QWidget>(object);
          if(widget != 0)
          {
          // object is QWidget or subclass
          ...
          }
          QLayoutItem layoutItem = qobject_cast<QLayoutItem>(object);
          if(layoutItem != 0)
          {
          // object is QLayoutItem or subclass
          ...
          }
          @

          1 Reply Last reply
          0
          • sierdzioS Offline
            sierdzioS Offline
            sierdzio
            Moderators
            wrote on last edited by
            #7

            You can use "QMetaObject::className()":http://developer.qt.nokia.com/doc/qt-4.8/qmetaobject.html#className

            Also, take a look at "QMetaObject":http://developer.qt.nokia.com/doc/qt-4.8/qmetaobject.html itself, and "QMetaClassInfo":http://developer.qt.nokia.com/doc/qt-4.8/qmetaclassinfo.html docs.

            (Z(:^

            1 Reply Last reply
            0
            • ? This user is from outside of this forum
              ? This user is from outside of this forum
              Guest
              wrote on last edited by
              #8

              So the solution is dynamic cast or qobject_cast() to every possible type until a valid pointer is returned? That doesn't sound too elegant.

              1 Reply Last reply
              0
              • L Offline
                L Offline
                lgeyer
                wrote on last edited by
                #9

                Yes. But you have to have different code paths for different types anyways, so I see no problem in that.

                There is no "name cast". This just doesn't work.
                @
                SomeObject* someObject = new SomeObject;
                ...
                // what type does anyPossibleObject have at compile time?
                ...* anyPossibleObject = name_cast<"anyPossibleTypeName">(someObject);

                // what interface does anyPossibleObject have at compile time?
                anyPossibleObject->...();
                @

                1 Reply Last reply
                0
                • ? This user is from outside of this forum
                  ? This user is from outside of this forum
                  Guest
                  wrote on last edited by
                  #10

                  Yes, this is the generic way to go, I was just curious if Qt provides some improvement as it does in many other areas.

                  I suppose it is possible to extend stock Qt classes with additional member variable to identify a set of pre-defined objects during runtime instead of casting one by one. Then a simple switch should be way faster than a chain of dynamic casts.

                  1 Reply Last reply
                  0
                  • G Offline
                    G Offline
                    goetz
                    wrote on last edited by
                    #11

                    You can use QObject::inherits(), QObject::className() and qobject_cast(). The Qt meta object system provides you the complete class hierarchy from the most specialized class name up to the toplevel QObject.

                    http://www.catb.org/~esr/faqs/smart-questions.html

                    1 Reply Last reply
                    0
                    • L Offline
                      L Offline
                      lgeyer
                      wrote on last edited by
                      #12

                      [quote author="ddriver" date="1330097642"]Yes, this is the generic way to go, I was just curious if Qt provides some improvement as it does in many other areas.

                      I suppose it is possible to extend stock Qt classes with additional member variable to identify a set of pre-defined objects during runtime instead of casting one by one. Then a simple switch should be way faster than a chain of dynamic casts.[/quote]
                      Well, you won't win anything ;-)

                      You always can identify QObject classes during runtime using QObject::metaObject()->className(), but in order to use it you will have to cast to it.

                      Each class has its unique interface (set of methods, members and properties) and in order to access this interface (invoke a method) you will have to use either a pointer of the class' type or the parent class' type, because C++ enforces strict typing. And this type has to be known at compile time.

                      So in order to distinct n different classes you will have in any case to cast n times.

                      If multiple object share the same interface they share the same base class, so they can be called through a base class pointer, to which you have to cast too.

                      Qt doesn't improve anything here because there is just nothing to improve. That's how C++ and any other non-interpreted language works ;-)

                      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