Suppose I have QObject* obj and I know it's a widget, how can I obtain the QWidget* widget corresponding to obj?

Qt Code:
  1. const QToolBox* tb = (const QToolBox*) parent;
  2. QObjectList *z = tb->queryList("QWidget", 0, FALSE, FALSE);
  3. QObjectListIt it(*z);
  4. QObject *obj;
  5. int index = 0;
  6. while ( (obj = it.current()) != 0 )
  7. {
  8. ++it;
  9. QRect tab = ( (QWidget*)obj )->rect();
  10. if ( tab.contains(childRect.center()) )
  11. {
  12. index = tb->indexOf( (QWidget*)obj );
  13. break;
  14. }
  15. }
  16. delete z;
To copy to clipboard, switch view to plain text mode 

I don't know why but in this example ( (QWidget*)obj )->rect() works but indexOf( (QWidget*)obj ) doesn't. index is always -1. So I'm looking for something to replace (QWidget*)obj and obtain the correct result.

AFAIK the rest is OK and works as expected but if you see a flaw, don't hesitate to say so. I've spent so much time staring at this code that I can't see anything.