Reputation: 7764
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 (QToolButton
s) 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.
Upvotes: 3
Views: 10291
Reputation: 6566
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
Upvotes: 0
Reputation: 10528
Call children()
on the top level widget instance.
Assuming your top level widget is called 'tlWidget':
myUI->tlWidget->children()
Upvotes: 4
Reputation: 20881
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.
Upvotes: 2