PDA

View Full Version : From QObject to QWidget



Maxilys
26th February 2006, 17:15
Suppose I have QObject* obj and I know it's a widget, how can I obtain the QWidget* widget corresponding to obj?



const QToolBox* tb = (const QToolBox*) parent;
QObjectList *z = tb->queryList("QWidget", 0, FALSE, FALSE);
QObjectListIt it(*z);
QObject *obj;
int index = 0;
while ( (obj = it.current()) != 0 )
{
++it;
QRect tab = ( (QWidget*)obj )->rect();
if ( tab.contains(childRect.center()) )
{
index = tb->indexOf( (QWidget*)obj );
break;
}
}
delete z;


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. :)

wysota
27th February 2006, 00:39
It's not the matter of casting that QObject -- it is done (more or less) correctly. You could be using dynamic_cast instead, but let's say it's ok. Looks like that widget is not a child of that toolbox. remember indexOf will return only direct children (in the meaning of "pages", not Qt parent-child relation) of the toolbox, not their children or any other widgets.

What exactly do you want to do?

Maxilys
27th February 2006, 20:27
"Pages" is exactly what I'm interested in, not the widgets they contain... and what I'm trying to do is to retrieve the index of the pages.

I'm writing a style plugin and when the time comes to draw a toolbox tab, all that Qt condescends to give me is a pointer to the toolbox widget (the parent of the tabs) and the QRect in which I'm supposed to draw one tab.

I would like to retrieve the widget corresponding to this QRect and then its index. It looks like it isn't as easy as one may think. QToolbox doesn't provide enough informations like QTabBar with its "regular" tabs... so I have to hack a workaround.

art
2nd February 2014, 18:07
I am not an expert in programming but all I could notice is that your QToolBox * tb has a const in front of it whcih means that you cannot change the data type hence no casting. That's where you invoke tb->indexOf where tb is of a consts datatype.