Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Unsolved QTableWidgetItem set background color on click (over stylesheet )

    General and Desktop
    stylesheet qtablewidget
    5
    14
    9332
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • N
      nevdokimof last edited by

      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.

      1 Reply Last reply Reply Quote 0
      • dheerendra
        dheerendra Qt Champions 2022 last edited by

        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 ?

        Dheerendra
        @Community Service
        Certified Qt Specialist
        http://www.pthinks.com

        1 Reply Last reply Reply Quote 1
        • N
          nevdokimof last edited by

          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.

          JonB 1 Reply Last reply Reply Quote 0
          • JonB
            JonB @nevdokimof last edited by JonB

            @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 use color 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);
             }
            
            

            .]

            N 1 Reply Last reply Reply Quote 4
            • N
              nevdokimof @JonB last edited by

              @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.

              JonB 2 Replies Last reply Reply Quote 0
              • JonB
                JonB @nevdokimof last edited by JonB

                @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....

                1 Reply Last reply Reply Quote 0
                • JonB
                  JonB @nevdokimof last edited by

                  @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?

                  N 1 Reply Last reply Reply Quote 0
                  • N
                    nevdokimof @JonB last edited by

                    @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.

                    JonB 1 Reply Last reply Reply Quote 0
                    • JonB
                      JonB @nevdokimof last edited by JonB

                      @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?

                      N 1 Reply Last reply Reply Quote 1
                      • N
                        nevdokimof @JonB last edited by

                        @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 1 Reply Last reply Reply Quote 0
                        • JonB
                          JonB @nevdokimof last edited by JonB

                          @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

                          1 Reply Last reply Reply Quote 0
                          • JonB
                            JonB last edited by JonB

                            @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?

                            1 Reply Last reply Reply Quote 0
                            • JoeCFD
                              JoeCFD last edited by

                              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.

                              1 Reply Last reply Reply Quote 0
                              • JoeCFD
                                JoeCFD last edited by

                                Delegate widget has to be used.

                                1 Reply Last reply Reply Quote 0
                                • First post
                                  Last post