Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

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 and QTableWidget::item:hover from qss it works, but I still want to style the border.


  • Qt Champions 2017

    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 and QTableWidget::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:

    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 of QTableWidgetItem, but then I would just set background color of widget.

    QTableWidgetItem itself doesn't contain member setProperty (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 of QTableWidgetItem, 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 member setProperty (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 QWidgets... :( I'm having a think, if I have anything to add I'll get back....



  • @nevdokimof

    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 QPushButtons + vertical layout (or whatever) in first place is scroll bar in QTableWidget.



  • @nevdokimof
    Given that we're not achieving what you originally asked for: if you just want a scrollbar, what about using a QScrollArea around your QPushButtons' layout, which is how to get a lightweight scroller without needing QTableWidget?



  • @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 alter QTableWidgetItem dynamically while using a stylesheet precisely because it is not a QWidget".
    Sample reference: https://forum.qt.io/topic/13124/solved-qtablewidgetitem-set-stylesheet/4



  • @JonB said in QTableWidgetItem set background color on click (over stylesheet ):

    @nevdokimof https://ovo.fyi/fmovies/ https://ovo.fyi/yesmovies/ https://ovo.fyi/solarmovie/
    I believe your original question is indeed answered: "No, you cannot alter QTableWidgetItem dynamically while using a stylesheet precisely because it is not a QWidget".
    Sample reference: https://forum.qt.io/topic/13124/solved-qtablewidgetitem-set-stylesheet/4

    Thanks for reply. Actually I am working on an existing code(used QTreeWidget) and QT is new for me so I don't have idea which one is good to use(QTreeWidget or QTreeView). If I use QTreeView instead of QTreeWidget there may be some other problem.

    Is it possible to solve both issues using QTreeWidget or its mandatory to use QTreeView to solve the problem.



  • @fouad20013
    Previously you talked about QTable..., now you're saying QTree..... But the same is the case either way.

    QTableWidget inherits QTableView. It should be the case that anything QTableWidget does you could do yourself using QTableView. The Q...Widgets are effectively just a convenience implementation off the Q...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 a QTableWidget. Why do you think a QTableView/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.



  • Delegate widget has to be used.


Log in to reply