Reputation: 917
How do I get a parent object by its name? QWidget::parentWidget()
goes only one level up; I rather need a recursive search equivalent to QObject::findChild()
.
Upvotes: 2
Views: 2200
Reputation: 28659
QWidget
inherits QObject
so QObject::objectName
is available to all QWidgets
You can recurse up the parent tree by creating a free function somewhat like the following (note this is untested code)
QWidget* parentByName(QWidget* widget, QString name)
{
widget = parentWidget();
if (widget && widget->objectName() != name)
return parentByName(widget, name);
return widget;
}
This will return either the widget you're looking for, or a nullptr in the event it can't be found
Upvotes: 4