tags:

views:

263

answers:

4

I have defined a UI (let's call it myUI) using the Qt designer, and using it in my applications. I need to access all the sub-widgets (QToolButtons) in myUI. I want to get all the subwidgets as a QObjectList.

Is there any way to do this?

The QObject::children() doesn't work here because the Qt UI Compiler, when converting the .ui file to a C++ class, doesn't define the ui_myUI class as a subclass of any QObject derived class. Is there any way to force it to do this, and then use the children() function?

Thanks.

+1  A: 

Usually what happens is that you either inherit from the UI class or you have it as a member and invoke it's setupUi method, sending this as the parameter. The default in Qt Creator/Designer is to have it as a member, named ui.
You can use this member to access any widgets defined in your form.

Idan K
The thing is, I want to get all the widgets defined in the form as a `QObjectList`. Is that possible?
trex279
oh, I misunderstood what you asked initially. So you want all QObject whos parent is your form. Wouldn't calling children() on your form give you that? After all the UI class adds children to that class. So if you have a class MyWindow that uses MyWindowUi, call children() on MyWindow. If I understand correctly that will give you what you want (although I'm pretty sure that will return the direct children)
Idan K
Turns out there is a level of indirection used in the ui_MyUI.cpp file. It defines it's own widget which is the parent of all the other widgets. Finally figured it out. Thanks anyways.
trex279
yes I forgot about that, it usually creates a widget and uses setCentralWidget on it.
Idan K
A: 

You might find this interesting:

BastiBense
Yeah I have seen that. And it doesn't really help. I've tried the multiple inheritance approach, but I'm not able to get the subwidgets as a `QObjectList` using `this->children()`
trex279
+2  A: 

Call children() on the top level widget instance.

Assuming your top level widget is called 'tlWidget':

myUI->tlWidget->children()

Ton van den Heuvel
Though this didn't solve my problem directly, it led me in the right direction. Turns out there is a level of indirection, and the actual parent of all the widgets is some other widget not defined by me.
trex279
A: 

How do you use your UI ?

(a) something like:

class MyWidget: public QWidget, protected myUI 
{
//...
};

(b) or rather something like:

class MyWidget: public QWidget
{
protected:
    myUI ui;
};

The Solution is similar for both cases, assumed that you call setupUi(this) or ui.setupUi(this) in the constructor of MyWidget

setupUi(QWidget* p) registers every widget of the UI as children of QWidget p, so you can easily access them by calling the children() function of p:

this->children(); //"this" refers to an object of MyWidget
smerlin