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

@@ -139,7 +139,7 @@ add_executable(Bloom
src/Insight/UserInterfaces/InsightWindow/Widgets/PanelWidget.cpp
src/Insight/UserInterfaces/InsightWindow/Widgets/RotatableLabel.cpp
src/Insight/UserInterfaces/InsightWindow/Widgets/SvgWidget.cpp
src/Insight/UserInterfaces/InsightWindow/Widgets/SvgToolButton.hpp
src/Insight/UserInterfaces/InsightWindow/Widgets/SvgToolButton.cpp
src/Insight/UserInterfaces/InsightWindow/Widgets/ClickableWidget.cpp
src/Insight/UserInterfaces/InsightWindow/Widgets/ExpandingHeightScrollAreaWidget.hpp

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);
}
}
}

View File

@@ -2,6 +2,7 @@
#include <QToolButton>
#include <QString>
#include <QChildEvent>
#include "SvgWidget.hpp"
@@ -17,10 +18,7 @@ namespace Bloom::Widgets
Q_PROPERTY(int buttonHeight READ getButtonHeight WRITE setButtonHeight DESIGNABLE true)
public:
explicit SvgToolButton(QWidget* parent): QToolButton(parent) {
this->setButtonWidth(10);
this->setButtonHeight(10);
};
explicit SvgToolButton(QWidget* parent);
void setSvgFilePath(const QString& svgFilePath) {
this->svgWidget->setSvgFilePath(svgFilePath);
@@ -58,6 +56,9 @@ namespace Bloom::Widgets
return this->buttonHeight;
}
protected:
void childEvent(QChildEvent* childEvent) override;
private:
SvgWidget* svgWidget = new SvgWidget(this);
int buttonWidth = 0;