c++ - Comparing QWidgets -
how can compare qwidgets? lets have list , change sizes of widgets qpushbutton
:
for example, there function or method this?
qlist<qlayoutitem *> itemlist; if(itemlist->items->type() == qpushbutton) { item.setgeometry(newrect); }
there several ways this, actually. qobject::inherits() one:
for (int = 0; < itemlist.size(); ++i) { if (itemlist[i]->inherits("qpushbutton")) { itemlist[i]->setgeometry(newrect); } }
another way using qobject_cast, recommend. returns null pointer if object not inherit (directly or not) class cast to:
for (int = 0; < itemlist.size(); ++i) { if (qobject_cast<qpushbutton*>(itemlist[i])) { itemlist[i]->setgeometry(newrect); } }
note qobject_cast not need rtti support enabled in compiler.
and there's of course standard c++ dynamic_cast need rtti support enabled:
for (int = 0; < itemlist.size(); ++i) { if (dynamic_cast<qpushbutton*>(itemlist[i])) { itemlist[i]->setgeometry(newrect); } }
another method appear ok @ first glance, should not used in case, qmetaobject::classname(). not use this, because you'll false negatives qpushbutton subclasses.
the reason recommend qobject_cast because allows compiler check typename. important when changing name of class. compiler isn't able check when using string-based qobject::inherits() function, code continue compile without problems won't work @ runtime.
Comments
Post a Comment