How better find class pointer by class name?



  • 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)?



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



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



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



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



  • [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.



  • [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...)


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.