How to set Bakground Color of QLabel while preserve the border-radius style
Unsolved
General and Desktop
-
I want to set the background color fade animation for QLabel
Here is my code#ifndef MYLABEL_H #define MYLABEL_H #include <QLabel> #include <QColor> #include <QPropertyAnimation> class MyLabel : public QLabel { Q_OBJECT Q_PROPERTY(QColor color READ getColor WRITE setColor) public: MyLabel(QWidget *parent = nullptr); void setColor(QColor color); QColor getColor(); protected: virtual void enterEvent(QEvent *event); private: QPropertyAnimation *animation = nullptr; }; #endif // MYLABEL_H
#include "mylabel.h" #include <QEasingCurve> #include <QPalette> MyLabel::MyLabel(QWidget *parent) :QLabel(parent) { animation = new QPropertyAnimation(this, "color"); animation->setDuration(1000); animation->setStartValue(QColor(0, 0, 0)); animation->setEndValue(QColor(240, 240, 240)); animation->setEasingCurve(QEasingCurve::BezierSpline); } void MyLabel::setColor(QColor color) { this->setAutoFillBackground(true); QPalette pal(this->palette()); pal.setColor(QPalette::Window, color); this->setPalette(pal); } QColor MyLabel::getColor() { return Qt::black; } void MyLabel::enterEvent(QEvent *) { animation->start(); }
However, if I set the border-radius, the QPalette will still fill the corner
Alternatively, if I just use the function setStyleSheet
void MyLabel::setColor(QColor color) { setStyleSheet(QString("background-color: rgb(%1, %2, %3);border-width:2px;border-style:solid;border-radius:10px;border-color:gray;").arg(color.red()).arg(color.green()).arg(color.blue())); }
It will work well. But this method looks stupid.
Is there any way let the first method work well?