Correct style aware custom widgets don´t get the style applied anyway
-
It always worked until now. I don´t know what's going on but i got this code of a QPushButton customized:
@
class SmartModeButton : public QPushButton
{private:
public:
SmartModeButton( ... );
protected:
void paintEvent(QPaintEvent *);
};
@And i have this method
@
SmartModeButton::SmartModeButton( ... ): QPushButton(parent_)
{
(...)setWindowFlags( Qt::Window | Qt::FramelessWindowHint );
setAttribute(Qt::WA_QuitOnClose, true);
setAttribute( Qt::WA_TranslucentBackground );}
(...)
void SmartModeButton::paintEvent(QPaintEvent * e)
{
QStyleOption opt;
opt.init(this);
QPainter p(this);
style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this);
}
@Adding this makes it work:
setStyleSheed("background-color:green");
But using a stylesheet as follows does nothing. It doesn´t understand the name of the widget, but i don´t get any errors.
@
/* ------------------ MODE BUTTONS ------------------------------------------*/QPushButton
{ background-color: white; }SmartModeButton
{ background-color: green; }
@It still shows a white background. So it understand that it´s a button... but not a SmartModeButton.
Why?
The funny thing, it used to work. I don´t know what i changed, but it has no sense for me.
Thanks a lot.
-
For your SmartModeButton you can set an objectName
@SmartModeButton->setObjectName("SmartModeButton");@
and then in yout css file you can specify
@QPushButton#SmartModeButton
{
background-color: green;
}@ -
Yeah. That´s an idea.
BUT, and it´s a big BUT... if i do that, i cannot distinguish the different SmartModeButtons between them.
I mean:
I have 4 buttons from that class. Then i set the name for each of them so i can set :
SmartModeButton#name1
SmartModeButton#name2
SmartModeButton#name3
SmartModeButton#name4Because i need different backgrounds for them, but the same behaviour. But by doing that, then all buttons are the same.
How would you do that?. And the big question: why did it stopped working??
If you have a custom class the stylesheet should work with it´s name just adding the paintEvent that way.
-
Any answers please?
-
For each instance of SmartModeButton you can set the objectName() and then accordingly you can change the background.
-
Sam, Thanks. but that´s the original question.
If you read the original post here, maybe you could see what´s going on. That is what is not working for me. It used to work but now it doesn´t. Doesn´t make sense, but it´s true.
-
Does your class declaration contain the Q_OBJECT macro? The reason I ask is that you are not showing it, but it is vital for this use case. If there is no Q_OBJECT macro, things like getting the class name for an object don't work properly, and thus style sheets also can't use that.
-
OK. that´s the answer i was thinking about.
YEAH. definitely that´s what i did.... i removed the Q_Object because i had nothing like slots or signals for my class, so i removed it.
THANKS A LOT
-
You're welcome.
Q_OBJECT does a lot more than just enable signals and slots. It is a core part of Qt's introspection mechanism. In general, I would advice to use the Q_OBJECT macro by default in every subclass you create of a QObject or class derived of QObject. That includes all widgets.
This way, you ensure that introspection features properly work. There are many scenarios where this is useful to have.