Palette overriding style sheet
-
Hi,
I am working on a desktop application. There is a
QListWidget
that contains custom widgets. These custom widgets are quite simple, they each contain just a few fields separated by vertical lines, each showing some text (QLabel
,QLCDNumber
, maybe a small flag icon).The point is, these widgets need to be created/changed quite quickly. They also need to change background/text color quite quickly. When I am changing color with
setStyleSheet
, it is not so fast (it is the bottleneck). When I useQPalette
, it runs multiple times faster. The problem is, we are using style sheets in the app, so a style sheet gets inevitably propagated to these widgets. Is there any way these 2 techniques can be combined? Would usingQStyle
override app style sheet and run faster? Is there any other way to speed this up? -
@aljoachim
So far as I understand it, stylesheet always overrides palette and that's how it works.I cannot comment on whether/why stylesheet is not fast enough for you. Maybe code could be optimized. Maybe using dynamic properties would help.
-
Hi,
It seems that your widgets are essentially read-only stuff. If so (and even if not), you would get better performance (and customization) by implementing a custom QStyledItemDelegate.
-
@SGaist thank you, that seems fine.
From what I understand, I have to reimplement QStyledItemDelegate::paint method, and draw the widget "manually", as for example here. Does that mean that I have to draw my widget "manually" or is there any simpler way to do this?
-
That's a pretty good example.
It might look complicate but less than you might think.
You work within the rectangle and paint what you want. The possibly most complicated would be the LCD. For that one you could use a QLcdWidget as part of your delegate and render it after updating its value.
-
@aljoachim said in Palette overriding style sheet:
From what I understand, I have to reimplement QStyledItemDelegate::paint method, and draw the widget "manually", as for example here. Does that mean that I have to draw my widget "manually" or is there any simpler way to do this?
I recently had a similar problem (using a
QStyleItemDelegate
myself). Inside the paint method I used this code for drawing:void MyDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &/*index*/) const { static QPushButton* dummyButton = new QPushButton(); // use this to force the correct style sheet QStyleOptionButton button; button.rect = option.rect; button.text = "Push Me"; button.state = QStyle::State_Enabled; const QWidget *widget = option.widget; QStyle *style = widget ? widget->style() : QApplication::style(); style->drawControl(QStyle::CE_PushButton, &button, painter, dummyButton); }
This was specifically to draw a push button. You have to figure out how this translates to the things you are trying to draw.
-
@SimonSchroeder
Hi Simon. Purely OoI, unlike you to "leak" a push button! Any reason why you didn't use, say,static QPushButton dummyButton;
[not sure you need thestatic
now] here? -
@JonB said in Palette overriding style sheet:
Any reason why you didn't use, say, static QPushButton dummyButton; here?
I guess I fumbled to long to actually get the style sheet to apply. I tried many different things (because I didn't see this documented anywhere). Must have overlooked this when cleaning up. Thank you! Will fix it in my code.