PDA

View Full Version : Dynamically change style sheeted QTableView row background color



topoden
20th May 2013, 21:57
I'm trying to implement 'blinking row background' in QTableView. I initially wanted to implement this by:


Using QTimeLine class to emit periodical updates for the QTableView
Sub-classing QStyledItemDelegate and re-implementing initStyleOption method by first calling its base class version and then amending QStyleOptionViewItemV4::backgroundBrush property


The problem I have is the QTableView is customized by style sheets. So the style class implementation does not use QStyleOptionViewItemV4::backgroundBrush property when its drawing the background.

What would be a proper approach for me to use in this case?

wysota
21st May 2013, 07:42
A proper approach would be to not overuse style sheets.

topoden
21st May 2013, 14:07
Can you please elaborate what you mean by "overuse stylesheets" here? There is already working application where stylesheets were used to give the application nice look. I thought it is exactly the way stylesheets are supposed to be used.

Now I have to make a minor change to the application (well I thought it would be minor, but apparently its not): make rows of one of the table views to blink in some special cases.

Do you think the only way now is to get rid of the stylesheets in the entire application?

wysota
21st May 2013, 15:45
Can you please elaborate what you mean by "overuse stylesheets" here?
I mean total restyling of your application only using stylesheets.


Now I have to make a minor change to the application (well I thought it would be minor, but apparently its not): make rows of one of the table views to blink in some special cases.
You can make them blink. They just won't follow stylesheets anymore. Hence my original answer.


Do you think the only way now is to get rid of the stylesheets in the entire application?
No, I mean you should style your table with a custom delegate and not stylesheets. You can use stylesheets for other things.

topoden
21st May 2013, 15:52
No, I mean you should style your table with a custom delegate and not stylesheets. You can use stylesheets for other things.

Did I get it right that for this particular table I need to disable stylesheets and do the entire drawing in the delegate? What would be the way to disable the stylesheets for a particular QTableView regardless of how complex parent widget's stylesheets are? The stylesheets can be cascading to the table from several parents. Do I have to amend selectors for all of them?

wysota
21st May 2013, 16:40
You don't need to disable stylesheets. Just don't style items or ignore that they do have any style. If you implement your own delegate and not call the base class implementation, items in the table will not be styled by stylesheets.

topoden
21st May 2013, 17:06
Got it, but is it possible to somehow work around implementing the entire drawing by myself but calling the base class's method with a hint not to use stylesheets? Say, in debugger I see it takes the style to draw with from QStyleOptionViewItemV3::widget property. Do you think it can be right to amend this property somehow in the sub-classed delegate paint method and then call base class's one?

wysota
21st May 2013, 23:17
If you use QItemDelegate instead of QStyledItemDelegate (which is the default) then stylesheets will not be used in items.