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. How better find class pointer by class name?
Forum Updated to NodeBB v4.3 + New Features

How better find class pointer by class name?

Scheduled Pinned Locked Moved General and Desktop
7 Posts 5 Posters 6.0k 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.
  • M Offline
    M Offline
    maxim.prishchepa
    wrote on last edited by
    #1

    I Have pointer to some class, then i need find some class(es) using this pointer(maybe current pointer is needed class, or parent pointer is needed class etc.) now i do that:
    @QMdiArea * mdi = NULL;
    QMainWindow * main = NULL;
    QObject * p = parent();
    while(p != NULL){
    QString str = p->metaObject()->className();
    if(str == "QMdiArea"){
    mdi = qobject_cast<QMdiArea *>(p);
    }
    if(str == "QMainWindow"){
    main = qobject_cast<QMainWindow *>(p);
    }
    p = p->parent();
    }
    if(mdi == NULL)
    return;
    if(main == NULL)
    return;@
    but if i want find 20\50\100 classes, i need write 20\50\100 if(str == "someClassName") ? i think it's not good "written style", maybe have some else variant to solve this problem?

    PS: is Qt have some text constants for classes name? i think write str == "SomeClass" - is not correct, because if class name will be changed current code, stop work :(
    Can i do something like that: find needed classes, add them to List<Object> and then when i needed get some class, i exec some function, where i point to needed class name, and this function will do some casts and return needed class (maybe write some template function or something else)?

    Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz).

    1 Reply Last reply
    0
    • L Offline
      L Offline
      ludde
      wrote on last edited by
      #2

      I think what you really should be asking yourself is why you need to do something like this. Seems like something could instead be improved in your object oriented design if you need to write this kind of code. Maybe not to just go to the top QMainWindow, but if you realy think you need 20\50\100 classes in there, it looks like you should reconsider your design instead.

      Also, do you really use QMainWindow directly, or do you use a subclass? In that case you should use inherits() and not metaObject()->className(). And p->inherits("QMainWindow") is also a lot less to write...

      1 Reply Last reply
      0
      • M Offline
        M Offline
        maxim.prishchepa
        wrote on last edited by
        #3

        i write this code for example and 20\50\100 - it's not real, but 3-5 is more then real :)

        I have my class inherited from QMainWindow and many others classes inherited from QDockWidget or QMdiArea etc., and at one of them i need create new dock widget with my widget inside, thats why i try find main window and others needed for me classes.

        Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz).

        1 Reply Last reply
        0
        • K Offline
          K Offline
          KA51O
          wrote on last edited by
          #4

          You could design a parent class for all those different classes you want to be able to identify. This class should have a member of type enum ClassType or something where you list all the different types of the sublasses you want to implement. Then give the parent class a method getClassType() that returns its ClassType. In every constructor of the derived classes you need to set its related ClassType and done. Now you can get the type of every class without having to know which subclass it is.

          1 Reply Last reply
          0
          • M Offline
            M Offline
            maxim.prishchepa
            wrote on last edited by
            #5

            Yes, but i don't believe so Qt can't do this himself :)
            [quote author="KA51O" date="1310460722"]You could design a parent class for all those different classes you want to be able to identify. This class should have a member of type enum ClassType or something where you list all the different types of the sublasses you want to implement. Then give the parent class a method getClassType() that returns its ClassType. In every constructor of the derived classes you need to set its related ClassType and done. Now you can get the type of every class without having to know which subclass it is.[/quote]

            Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz).

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

              [quote author="Maxim Prishchepa" date="1310460185"]I have my class inherited from QMainWindow and many others classes inherited from QDockWidget or QMdiArea etc., and at one of them i need create new dock widget with my widget inside, thats why i try find main window and others needed for me classes.[/quote]

              Just store a globally accessibly pointer to your main window somewhere (ie. by adding a static function to your QMainWindow subclass which returns the current instance).

              For accessing sub widgets of your main window you should consider providing getters (as QMainWindow for example does with QMainWindow::menuBar() or QMainWindow::statusBar()). If you create your widgets on the fly another possibility would be assinging object names to them (QObject::setObjectName()) and use "QObject::findChild()":http://doc.qt.nokia.com/latest/qobject.html#findChild to find them.

              As others stated this is just a matter of design. A common indicator that your design is possibly flawed is that you want to / have to do something that is not directly supported by Qt.

              1 Reply Last reply
              0
              • G Offline
                G Offline
                giesbert
                wrote on last edited by
                #7

                [quote author="Maxim Prishchepa" date="1310458609"]I Have pointer to some class, then i need find some class(es) using this pointer(maybe current pointer is needed class, or parent pointer is needed class etc.) now i do that:
                @QMdiArea * mdi = NULL;
                QMainWindow * main = NULL;
                QObject * p = parent();
                while(p != NULL){
                QString str = p->metaObject()->className();
                if(str == "QMdiArea"){
                mdi = qobject_cast<QMdiArea *>(p);
                }
                if(str == "QMainWindow"){
                main = qobject_cast<QMainWindow *>(p);
                }
                p = p->parent();
                }
                if(mdi == NULL)
                return;
                if(main == NULL)
                return;@
                [/quote]

                I would not use the class name to retreive this. JUst use a qobject_cast. If it returns 0, it did not fir. Then you are string compare free in your code (even qobject_cast does some internally...)

                Nokia Certified Qt Specialist.
                Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

                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