[UI] push button update

- Add child widget existency check when initializing UI
- Prevent double triggering `onClick` signal
- Add check on set child widget API
This commit is contained in:
Linloir 2022-12-16 11:12:08 +08:00
parent e88498cf20
commit 0bddabdf80
No known key found for this signature in database
GPG Key ID: 58EEB209A0F2C366
2 changed files with 20 additions and 7 deletions

View File

@ -23,9 +23,13 @@ void PushButton::initializeUI() {
_stretchLayout->setContentsMargins(_contentMargin); _stretchLayout->setContentsMargins(_contentMargin);
_stretchLayout->setSpacing(0); _stretchLayout->setSpacing(0);
_stretchLayout->setAlignment(Qt::AlignCenter); _stretchLayout->setAlignment(Qt::AlignCenter);
_stretchLayout->addWidget(_childWidget);
setLayout(_stretchLayout); setLayout(_stretchLayout);
if (_childWidget) {
// prevent adding a null widget
_stretchLayout->addWidget(_childWidget);
_childWidget->show(); _childWidget->show();
_childWidgetOriginalGeometry = _childWidget->geometry();
}
// Initialize background widget // Initialize background widget
_backgroundWidget = new QWidget(this); _backgroundWidget = new QWidget(this);
@ -241,6 +245,7 @@ void PushButton::mousePressEvent(QMouseEvent* event) {
indicatorShrinkLength->start(QAbstractAnimation::DeleteWhenStopped); indicatorShrinkLength->start(QAbstractAnimation::DeleteWhenStopped);
_pressed = true; _pressed = true;
emit onPressed(); emit onPressed();
} }
@ -291,11 +296,14 @@ void PushButton::mouseReleaseEvent(QMouseEvent* event) {
} }
indicatorGrowLength->start(QAbstractAnimation::DeleteWhenStopped); indicatorGrowLength->start(QAbstractAnimation::DeleteWhenStopped);
_pressed = false; if (_pressed) {
// prevent double trigger if mouse is first left and then released
emit onClick(); emit onClick();
} }
_pressed = false;
}
void PushButton::resizeEvent(QResizeEvent* event) { void PushButton::resizeEvent(QResizeEvent* event) {
_backgroundWidget->setGeometry(0, 0, width(), height()); _backgroundWidget->setGeometry(0, 0, width(), height());
switch (_indicatorPosition) { switch (_indicatorPosition) {
@ -790,8 +798,11 @@ void PushButton::setChildWidget(QWidget* widget) {
for (int i = 0; i < _stretchLayout->count(); i++) { for (int i = 0; i < _stretchLayout->count(); i++) {
_stretchLayout->removeItem(_stretchLayout->itemAt(i)); _stretchLayout->removeItem(_stretchLayout->itemAt(i));
} }
if (_childWidget != nullptr) {
_stretchLayout->addWidget(_childWidget); _stretchLayout->addWidget(_childWidget);
_childWidget->show(); _childWidget->show();
_childWidgetOriginalGeometry = _childWidget->geometry();
}
} }
bool PushButton::isSelected() const { bool PushButton::isSelected() const {

View File

@ -19,12 +19,14 @@ public:
}; };
public: public:
PushButton(QWidget* child, QWidget* parent = 0); PushButton(QWidget* child, QWidget* parent);
~PushButton(); ~PushButton();
private: private:
// Button Layout // Button Layout
QWidget* _childWidget; QWidget* _childWidget;
QRect _childWidgetOriginalGeometry;
const float _childWidgetShrinkRatio = 0.9;
QHBoxLayout* _stretchLayout; QHBoxLayout* _stretchLayout;
QMargins _contentMargin = QMargins(12, 12, 12, 12); QMargins _contentMargin = QMargins(12, 12, 12, 12);