QTableWidgetItem set background color on click (over stylesheet )
-
Is there a way to set background color for QTableWidgetItem dynamically while using stylesheet for them?
Here is my qss:
QTableWidget { border: 1px solid #cccccc; border-radius: 8px; } QTableWidget::item { border-bottom: 1px solid #cccccc; background-color: qlineargradient(spread:pad, x1:1, y1:1, x2:1, y2:0, stop:0 rgba(245, 245, 245, 245), stop:1 rgba(255, 255, 255, 255)); } QTableWidget::item:hover { border: 1px solid #F37021; border-radius: 8px; background-color: #ffffff; }
I want to change item background color when user clicks on it. What I've tried so far:
QTableWidget::item:pressed
ain't working.void MainWindow::on_tableWidget_itemClicked(QTableWidgetItem *item) { item->setData(Qt::BackgroundRole, QColor(243,112,33)); //or item->setBackgroundColor(QColor(243,112,33)); }
neither (it can't override stylesheet I believe). As soon as I remove
QTableWidget::item
andQTableWidget::item:hover
from qss it works, but I still want to style the border. -
What happening for you when you handled the signals itemDoubleClicked or itemPressed or itemClicked ? Is it not changing the color ? Is it not calling the slot ?
-
Signals emitted and received as expected. But color isn't being changed.
When I remove styles for
QTableWidget::item
andQTableWidget::item:hover
from stylesheet file, color changes. -
Signals emitted and received as expected. But color isn't being changed.
When I remove styles for
QTableWidget::item
andQTableWidget::item:hover
from stylesheet file, color changes.@nevdokimof
So far as I understand it, the whole point is that stylesheet entries override whatever you try to do in code, so you won't achieve it that way (unless @dheerendra knows better than I).When I need to do this, I do it by:
- Set a dynamic property on the item in code (I use a dynamic property named
class
, personally; you might usecolor
or whatever for this). See https://wiki.qt.io/Dynamic_Properties_and_Stylesheets & http://doc.qt.io/qt-5/stylesheet-examples.html#customizing-using-dynamic-properties. - Create rules in the stylesheet for the various property's values to produce the desired effect.
Is that what you would like to do?
[P.S. Funnily enough, I happen to have just looked at a new post https://forum.qt.io/topic/97783/style-text-in-qtextedit/4, that is using exactly the principle I use, saved me going find an example in my own code! You would adapt for your various
QTableWidget::item
rules, but that is the coding principle I recommend://StyleSheet QTextEdit[styleVariant="1"] { // Custom Style } QTextEdit{ // Default Style } // connect pushbutton clicked(bool checked = false) signal to onPushButtonClicked onPushButtonClicked(bool checked) { if(checked) textEdit->setProperty("styleVariant", 1); else textEdit->setProperty("styleVariant", 0); }
.]
- Set a dynamic property on the item in code (I use a dynamic property named
-
@nevdokimof
So far as I understand it, the whole point is that stylesheet entries override whatever you try to do in code, so you won't achieve it that way (unless @dheerendra knows better than I).When I need to do this, I do it by:
- Set a dynamic property on the item in code (I use a dynamic property named
class
, personally; you might usecolor
or whatever for this). See https://wiki.qt.io/Dynamic_Properties_and_Stylesheets & http://doc.qt.io/qt-5/stylesheet-examples.html#customizing-using-dynamic-properties. - Create rules in the stylesheet for the various property's values to produce the desired effect.
Is that what you would like to do?
[P.S. Funnily enough, I happen to have just looked at a new post https://forum.qt.io/topic/97783/style-text-in-qtextedit/4, that is using exactly the principle I use, saved me going find an example in my own code! You would adapt for your various
QTableWidget::item
rules, but that is the coding principle I recommend://StyleSheet QTextEdit[styleVariant="1"] { // Custom Style } QTextEdit{ // Default Style } // connect pushbutton clicked(bool checked = false) signal to onPushButtonClicked onPushButtonClicked(bool checked) { if(checked) textEdit->setProperty("styleVariant", 1); else textEdit->setProperty("styleVariant", 0); }
.]
@JonB this would be the thing if I could get
QWidget
presentation ofQTableWidgetItem
, but then I would just set background color of widget.QTableWidgetItem
itself doesn't contain membersetProperty
(or similar), so I can't achieve what I want in your way. Thanks for advice tho, didn't know about widget dynamic properties. - Set a dynamic property on the item in code (I use a dynamic property named
-
@JonB this would be the thing if I could get
QWidget
presentation ofQTableWidgetItem
, but then I would just set background color of widget.QTableWidgetItem
itself doesn't contain membersetProperty
(or similar), so I can't achieve what I want in your way. Thanks for advice tho, didn't know about widget dynamic properties.@nevdokimof said in QTableWidgetItem set background color on click (over stylesheet ):
@JonB this would be the thing if I could get
QWidget
presentation ofQTableWidgetItem
, but then I would just set background color of widget.Even then I thought that would not work as your stylesheet rule would override your code setting of background color?
QTableWidgetItem
itself doesn't contain membersetProperty
(or similar), so I can't achieve what I want in your way. Thanks for advice tho, didn't know about widget dynamic properties.Darn, I forgot those are not
QWidget
s... :( I'm having a think, if I have anything to add I'll get back.... -
@JonB this would be the thing if I could get
QWidget
presentation ofQTableWidgetItem
, but then I would just set background color of widget.QTableWidgetItem
itself doesn't contain membersetProperty
(or similar), so I can't achieve what I want in your way. Thanks for advice tho, didn't know about widget dynamic properties.I want to change item background color when user clicks on it.
When user clicks that "selects" the item, doesn't it? So have you tried
QTableWidget::item:selected
rule instead? -
I want to change item background color when user clicks on it.
When user clicks that "selects" the item, doesn't it? So have you tried
QTableWidget::item:selected
rule instead?@JonB I don't want to allow user "select" (as Qt means it) items. In my case items are supposed to behave like regural push buttons.
The reason why I don't use
QPushButton
s + vertical layout (or whatever) in first place is scroll bar inQTableWidget
. -
@JonB I don't want to allow user "select" (as Qt means it) items. In my case items are supposed to behave like regural push buttons.
The reason why I don't use
QPushButton
s + vertical layout (or whatever) in first place is scroll bar inQTableWidget
.@nevdokimof
Given that we're not achieving what you originally asked for: if you just want a scrollbar, what about using aQScrollArea
around yourQPushButton
s' layout, which is how to get a lightweight scroller without needingQTableWidget
? -
@nevdokimof
Given that we're not achieving what you originally asked for: if you just want a scrollbar, what about using aQScrollArea
around yourQPushButton
s' layout, which is how to get a lightweight scroller without needingQTableWidget
?@JonB yeah, that might be it. I'll try this way, you helped me a lot, thank you.
Original question is still unanswered though, would be interesting to know if there is a way to do what I've described in first post.
-
@JonB yeah, that might be it. I'll try this way, you helped me a lot, thank you.
Original question is still unanswered though, would be interesting to know if there is a way to do what I've described in first post.
@nevdokimof
I believe your original question is indeed answered: "No, you cannot alterQTableWidgetItem
dynamically while using a stylesheet precisely because it is not aQWidget
".
Sample reference: https://forum.qt.io/topic/13124/solved-qtablewidgetitem-set-stylesheet/4 -
@fouad20013
Previously you talked aboutQTable...
, now you're sayingQTree....
. But the same is the case either way.QTableWidget
inheritsQTableView
. It should be the case that anythingQTableWidget
does you could do yourself usingQTableView
. TheQ...Widget
s are effectively just a convenience implementation off theQ...View
, they provide an "item-based table view with a default model" if that's what you want.However, I think you will have the same issues whether you use a
QTableView
or aQTableWidget
. Why do you think aQTableView
/QTreeView
would solve your issue? -
I came cross the same problem. Any solution? After stylesheet is set for QTableWidgetItem, it seems impossible to change anything for example: background color. "QTableView::item{color:%5;border-color:%6;border:0px;padding-left:%7px;}" is used in qtablewidget and I would like to set different background color for every another row. Without stylesheet, padding can not be set.