Automatically associate SvgToolButton widgets to child QMenu. This means we can add a QMenu to a tool button, via the .ui file, and have it be automatically associated.

This commit is contained in:
Nav
2021-12-04 21:23:58 +00:00
parent 97af9b5b83
commit 7108899884
3 changed files with 29 additions and 5 deletions

View File

@@ -0,0 +1,23 @@
#include "SvgToolButton.hpp"
#include <QMenu>
using namespace Bloom::Widgets;
SvgToolButton::SvgToolButton(QWidget* parent): QToolButton(parent) {
this->setButtonWidth(10);
this->setButtonHeight(10);
}
void SvgToolButton::childEvent(QChildEvent* childEvent) {
if ((childEvent->added() || childEvent->polished()) && childEvent->child()->isWidgetType()) {
/*
* If a menu widget has been added as a child to this SvgToolButton, associate the menu with the button
* via QToolButton::setMenu().
*/
auto* menuWidget = qobject_cast<QMenu*>(childEvent->child());
if (menuWidget != nullptr && menuWidget != this->menu()) {
this->setMenu(menuWidget);
}
}
}