Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QTableWidgetItem set background color on click (over stylesheet )
Forum Updated to NodeBB v4.3 + New Features

QTableWidgetItem set background color on click (over stylesheet )

Scheduled Pinned Locked Moved Unsolved General and Desktop
stylesheetqtablewidget
14 Posts 5 Posters 11.9k Views
  • 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.
  • JonBJ 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 Offline
    N Offline
    nevdokimof
    wrote on last edited by
    #5

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

    JonBJ 2 Replies Last reply
    0
    • N nevdokimof

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

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by JonB
      #6

      @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
      0
      • N nevdokimof

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

        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on last edited by
        #7

        @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
        0
        • JonBJ JonB

          @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 Offline
          N Offline
          nevdokimof
          wrote on last edited by
          #8

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

          JonBJ 1 Reply Last reply
          0
          • N nevdokimof

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

            JonBJ Offline
            JonBJ Offline
            JonB
            wrote on last edited by JonB
            #9

            @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
            1
            • JonBJ 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 Offline
              N Offline
              nevdokimof
              wrote on last edited by
              #10

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

              JonBJ 1 Reply Last reply
              0
              • N nevdokimof

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

                JonBJ Offline
                JonBJ Offline
                JonB
                wrote on last edited by JonB
                #11

                @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
                0
                • JonBJ Offline
                  JonBJ Offline
                  JonB
                  wrote on last edited by JonB
                  #12

                  @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
                  0
                  • JoeCFDJ Offline
                    JoeCFDJ Offline
                    JoeCFD
                    wrote on last edited by
                    #13

                    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
                    0
                    • JoeCFDJ Offline
                      JoeCFDJ Offline
                      JoeCFD
                      wrote on last edited by
                      #14

                      Delegate widget has to be used.

                      1 Reply Last reply
                      0

                      • Login

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • Users
                      • Groups
                      • Search
                      • Get Qt Extensions
                      • Unsolved