Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Qt get stylesheet border color

Tags:

c++

qt

QPushButton#PBack {
    background: #9A9AFF;
    font: bold 28pt "Brutal Type";
    color: #000000;
    border: 3px solid #000000;
    border-radius: 25px;
}

QPushButton#PBack:disabled {
     background-color: #bdbdbd;
    border-color: #787878;
}

How to get border-color inside of paintEvent?

void W_PBackButton::paintEvent(QPaintEvent* event)
{
    // Here i use constants
    if (isEnabled() == true)
        painter.setPen(QPen(QBrush(_enabled), 3));
    else
        painter.setPen(QPen(QBrush(_disabled), 3));

    painter.fillPath(path, palette().base());
    painter.drawPath(path);
}

In example i use constants, but how to get color values from palette or styles?

Need to draw triangle button
enter image description here

like image 223
goldstar Avatar asked Sep 19 '25 16:09

goldstar


1 Answers

Now, given the fact question is filled in with more information, I think I can provide some answer. Well, I do not think you need to try getting some "border" property which is declared by Qt. It would be much more difficult. Instead, you can define your own custom property, e.g. "borderColor" (or a couple of them, such for thickness, shape, and so on), and set it like this through stylesheet: "qproperty-borderColor". About definition of custom properties, you can read more information here: https://doc.qt.io/qt-5/properties.html (and an example would be https://doc.qt.io/qt-5/properties.html#a-simple-example ).

The following code illustrates possible way of setting property desired. First of all, you would have to define your own class inheriting the class on which you want property to be set. Let's say it is child of QPushButton:

#include <QPushButton>

class CustomButton
    : public QPushButton
{
    Q_OBJECT
    Q_PROPERTY(QColor borderColor READ GetBorderColor WRITE SetBorderColor)
public:
    CustomButton(QWidget *parent = nullptr);
    virtual ~CustomButton();

    QColor GetBorderColor() const;
    void SetBorderColor(const QColor& color);

protected:
    virtual void paintEvent(QPaintEvent* event) override;

private:
    QColor m_borderColor;
};

#include "CustomButton.h"

#include <QDebug>

CustomButton::CustomButton(QWidget *parent)
    : QPushButton(parent)
{
}

CustomButton::~CustomButton()
{
}

QColor CustomButton::GetBorderColor() const
{
    return m_borderColor;
}

void CustomButton::SetBorderColor(const QColor& color)
{
    m_borderColor = color;

    qDebug() << m_borderColor;
}

void CustomButton::paintEvent(QPaintEvent* event)
{
    QPushButton::paintEvent(event);

    // Do whatever you like with m_borderColor and any other properties.
}

And finally, you would have to promote you QPushButton in the Qt Creator to your own CustomButton. Regarding widgets promotion, you can read about it here: https://doc.qt.io/archives/qt-4.8/designer-using-custom-widgets.html#promoting-widgets

And when you have everything setup, you can easily declare stylesheet for your newly introduced button:

#pushButton {
    qproperty-borderColor: #123456;
}

Maybe it is not the way you have intented to achieve your goal initially, but it looks like that declaring custom stylesheet properties and using promotion of widgets would help you achieve your goal without any hacking and in a way Qt was been built to be used. Hopefully, it helps you. :)

like image 110
Vaidotas Strazdas Avatar answered Sep 21 '25 07:09

Vaidotas Strazdas