Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to set Bakground Color of QLabel while preserve the border-radius style
Qt 6.12 LTS is out! See what's new in the release blog

How to set Bakground Color of QLabel while preserve the border-radius style

Scheduled Pinned Locked Moved Unsolved General and Desktop
1 Posts 1 Posters 368 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    lautreamont
    wrote on last edited by
    #1

    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
    1bf070ad-9732-48f6-9fc3-67b77677ee00-image.png

    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()));
    }
    

    03286b73-5758-4694-b48a-5ae668c7d402-image.png
    It will work well. But this method looks stupid.
    Is there any way let the first method work well?

    1 Reply Last reply
    0

    • Login

    • Login or register to search.
    • First post
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Get Qt
    • Unsolved