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. Item view (styleSheet bgcolor)
Qt 6.11 is out! See what's new in the release blog

Item view (styleSheet bgcolor)

Scheduled Pinned Locked Moved General and Desktop
12 Posts 5 Posters 10.6k Views 1 Watching
  • 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.
  • I Offline
    I Offline
    Immii
    wrote on last edited by
    #1

    hi all,
    I have QTreeWidget and QTreeWidgetItem. I already have a stylesheet applied to my tree widget making background color of all items Yellow, myTree.setStyleShett("QTreeWidget::item{background-color:yellow}")

    Now on a button click some of the QtreeWidgetItem text changes and I want to change the background color from yellow to GREEN of those Item whose text changed to "Passed" and RED to those items whose text become "Failed"

    I tried using setBackground(col,myGreenBrush) of those item whose text are Passed but it does not do any thing, I found styleSheet(QTreeWidget::item::text....) But I dont know how to use "::text" thing to compare my text in styleSheet.

    Can any one please help ?

    1 Reply Last reply
    0
    • J Offline
      J Offline
      Jake007
      wrote on last edited by
      #2

      Hi!

      ::text selector will apply only on text. You probably want to set it on ::item ( one item in QTreeWidget).

      Try setting QTreeWidget::item { background: #F00; } only on failed items and green on succeeded.

      Regards,
      Jake


      Code is poetry

      1 Reply Last reply
      0
      • I Offline
        I Offline
        Immii
        wrote on last edited by
        #3

        Jake: Thats what my question is:

        Using stylesheet how to apply background color on only those item which has Text "Failed".

        styleSheet() api is only available to QTreeWidget not on QTreeWidgetItem.

        Can any one please help?

        1 Reply Last reply
        0
        • S Offline
          S Offline
          Sam
          wrote on last edited by
          #4

          Hi,

          You can look for adding dynamic properties to stylesheets. I added a "Doc note":http://qt-project.org/doc/qt-5.0/qtwidgets/stylesheet-syntax.html#note-238 it may be helpful.

          1 Reply Last reply
          0
          • I Offline
            I Offline
            Immii
            wrote on last edited by
            #5

            Sam, This is really kool i like this but unfortunately This wont help me because QTreeWidgetItem is not derived from QOBject to have any property.

            Just to rephrase my problem, In my QtreeWidget there are only 20 items (items are QtreewidgetItem) and out of 20 item 2 items text are "failed" 15 items text are "passed" , I simply want to change background color of at least 2 failed items using StyleSheet.

            Any other idea?

            1 Reply Last reply
            0
            • J Offline
              J Offline
              Jake007
              wrote on last edited by
              #6

              Hi!

              I see your problem. I did not check the docs first. Sorry.

              @myTreeWidgetItem->setBackground(0, QBrush(Qt::Red, Qt::SolidPattern));@

              This should work.
              Tested on a demo project.

              Docs "here":http://doc.qt.digia.com/qt/qtreewidgetitem.html#setBackground

              Regards,
              Jake


              Code is poetry

              1 Reply Last reply
              0
              • I Offline
                I Offline
                Immii
                wrote on last edited by
                #7

                Jake: Thanx for looking into this but I have already tried that and mentioned in my first post that it does not work if an styleSheet is already Applied to QTreeWidget.

                it works on your demo because there is no stylesheet applied there before you call setBackground() if you already have applied setStyleSheet(QTreeWidget::item{background-color:yellow}) to make all item yellow, Now if you do QTreeWidgetItem *item=treeWidget.topLevelItem(2);item->setBackground(...) it has no effect.

                I am not sure if this is bug or what but it does not work, but if change the background using stylesheet itself on mouse hover background changes again to new color but this is happening only through stylesheet.
                and this is reason why I am looking for a way to compare if any item of my treewidget has text "Pass" i can only change the bacground color of that item to Green using STYLESHEET ONLY.

                I will really appreciate if you can suggest something

                1 Reply Last reply
                0
                • S Offline
                  S Offline
                  Sam
                  wrote on last edited by
                  #8

                  Hi,

                  If you are using stylesheets just for styling a QTreeWidgetItem, then i would suggest to use a delegate to get the result for the above scenario. You can write a class that inherits QStyledItemDelegate then override the paintEvent().

                  "Here":http://www.mediafire.com/?qmsdjdwxsxngits is a sample code.

                  1 Reply Last reply
                  0
                  • I Offline
                    I Offline
                    Immii
                    wrote on last edited by
                    #9

                    Sam: well the example code you pointed does not work.

                    Item setBackground() does not do any thing if an stylesheet is already applied. (if you do ui->treeWidget->setStyleSheet("QTreeWIdget::item{background-color=blue;}") then non of the yellow red green painting of paint function in delegate class has any effect on it.

                    and this what I am fighting with, I have to have my styleSheet on my items because of many reason specially gradient.

                    and on some condition I want to change the background color of few items, even using style delegat it does not help.

                    can any one please help

                    1 Reply Last reply
                    0
                    • S Offline
                      S Offline
                      Sam
                      wrote on last edited by
                      #10

                      Did you get this working, I would like to get an update on this one, If there is any possibility to achieve the same effect using stylesheets.

                      1 Reply Last reply
                      0
                      • A Offline
                        A Offline
                        AlekseyOk
                        wrote on last edited by
                        #11

                        Try to add custom dynamic properties "Failed", "Passed" or something else:

                        @
                        QTreeWIdget::item[state="failed"],
                        {
                        background-color: red;
                        }
                        QTreeWIdget::item[state="passed"],
                        {
                        background-color: green;
                        }
                        @

                        1 Reply Last reply
                        0
                        • raven-worxR Offline
                          raven-worxR Offline
                          raven-worx
                          Moderators
                          wrote on last edited by
                          #12

                          for people who will have the same issue in the future:

                          It seems that whenever you use a stylesheet definition for items
                          @
                          QTreeWIdget::item {
                          ...
                          }
                          @
                          they will have a higher priority than the one you have set (e.g. via the style option object or via the model and it's background role).

                          So the only solution to this will be not to style any border and/or background via stylesheet using the ::item selector.

                          --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
                          If you have a question please use the forum so others can benefit from the solution in the future

                          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