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

View File

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