Added toggle switch

This commit is contained in:
2026-01-15 21:26:10 +01:00
parent 291695bcb9
commit c6c058279a
2 changed files with 21 additions and 14 deletions

View File

@@ -1,12 +1,12 @@
#include "bctoggleswitch.h"
#include <QPainter> #include <QPainter>
#include <QPropertyAnimation> #include <QPropertyAnimation>
#include <QEasingCurve> #include <QEasingCurve>
#include <QEnterEvent> #include <QEnterEvent>
#include <QEvent> #include <QEvent>
#include <bctoggleswitch.h>
BCToggleSwitch::BCToggleSwitch(QWidget *parent) BCToggleSwitch::BCToggleSwitch(QWidget *parent)
: QAbstractButton(parent) : QAbstractButton(parent)
, m_position(0.0f) , m_position(0.0f)
@@ -34,7 +34,8 @@ float BCToggleSwitch::position() const
return m_position; return m_position;
} }
void BCToggleSwitch::setPosition(float pos) { void BCToggleSwitch::setPosition(float pos)
{
m_position = pos; m_position = pos;
update(); // Trigger Repaint update(); // Trigger Repaint
} }
@@ -52,9 +53,9 @@ void BCToggleSwitch::paintEvent(QPaintEvent *)
// --- Farben --- // --- Farben ---
// Tipp: In einem echten Projekt diese Farben als const statics // Tipp: In einem echten Projekt diese Farben als const statics
// oder aus der QPalette laden. // oder aus der QPalette laden.
QColor offBorderColor = QColor("#8D8D8D"); QColor offBorderColor = QColor(0x8D8D8D);
QColor offKnobColor = QColor("#5D5D5D"); QColor offKnobColor = QColor(0x5D5D5D);
QColor onColor = QColor("#0078D4"); // Fluent Blue QColor onColor = QColor(0x0078D4); // Fluent Blue
QColor white = Qt::white; QColor white = Qt::white;
QRectF rect = this->rect(); QRectF rect = this->rect();
@@ -95,11 +96,16 @@ void BCToggleSwitch::paintEvent(QPaintEvent *)
QRectF knobRect(currentX, padding, knobDiameter, knobDiameter); QRectF knobRect(currentX, padding, knobDiameter, knobDiameter);
if (isChecked() || m_position > 0.5f) { if (isChecked() || m_position > 0.5f)
{
p.setBrush(white); p.setBrush(white);
} else { }
if (underMouse()) p.setBrush(offKnobColor.darker(110)); else
else p.setBrush(offKnobColor); {
if (underMouse())
p.setBrush(offKnobColor.darker(110));
else
p.setBrush(offKnobColor);
} }
p.setPen(Qt::NoPen); p.setPen(Qt::NoPen);

View File

@@ -3,6 +3,7 @@
#include <QAbstractButton> #include <QAbstractButton>
// Vorwärtsdeklaration spart Include-Zeit // Vorwärtsdeklaration spart Include-Zeit
class QPropertyAnimation; class QPropertyAnimation;
@@ -28,10 +29,10 @@ protected:
void enterEvent(QEnterEvent *event) override; void enterEvent(QEnterEvent *event) override;
void leaveEvent(QEvent *event) override; void leaveEvent(QEvent *event) override;
private:
float m_position; // 0.0 = Aus, 1.0 = An float m_position; // 0.0 = Aus, 1.0 = An
QPropertyAnimation* m_animation; QPropertyAnimation* m_animation;
}; };
#endif // BCTOGGLESWITCH_H #endif // BCTOGGLESWITCH_H