diff --git a/bctoggleswitch.cpp b/bctoggleswitch.cpp index f5facb4..1c99cc8 100644 --- a/bctoggleswitch.cpp +++ b/bctoggleswitch.cpp @@ -1,12 +1,12 @@ +#include "bctoggleswitch.h" + #include #include #include #include #include -#include - 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); diff --git a/bctoggleswitch.h b/bctoggleswitch.h index 24d6ade..8997691 100644 --- a/bctoggleswitch.h +++ b/bctoggleswitch.h @@ -3,6 +3,7 @@ #include + // Vorwärtsdeklaration spart Include-Zeit class QPropertyAnimation; @@ -28,10 +29,10 @@ protected: void enterEvent(QEnterEvent *event) override; void leaveEvent(QEvent *event) override; - float m_position; // 0.0 = Aus, 1.0 = An - QPropertyAnimation *m_animation; +private: + + float m_position; // 0.0 = Aus, 1.0 = An + QPropertyAnimation* m_animation; }; - - #endif // BCTOGGLESWITCH_H