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. Color priority between stylesheets and QBrush.
Forum Updated to NodeBB v4.3 + New Features

Color priority between stylesheets and QBrush.

Scheduled Pinned Locked Moved Solved General and Desktop
8 Posts 4 Posters 869 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.
  • Dummie1138D Offline
    Dummie1138D Offline
    Dummie1138
    wrote on last edited by Dummie1138
    #1

    Hi. I have code that greys out the ui, including a QTableWidget. This is the sequence of the code:

    fillCurrentTable();
    ui_name::setEnabled(!greyState);
    ui->tableWidget->setEnabled(!greyState);
    ui->calibrationValue->setEnabled(!greyState);
    ui->tableWidget->setStyleSheet("font: 22px; color: grey;");
    
    void ResistanceCalibration::fillCurrentTable(){
            QBrush stableColor;
            stableMessage->setForeground(Qt::darkGreen);
            ui->tableWidget->setItem(x, 3, stableMessage);
    }
    

    QBrush is run before setStyleSheet in the code. Therefore I would have thought sSS would override QBrush and paint everything in the QTable grey. However, this did not happen.

    The result:
    8191e570-79f1-4faf-b916-6daa7942a93a-image.png

    Does QBrush take priority under most circumstances? Please let me know if more information is required.

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

      I would have guessed the same as @Christian-Ehrlicher. Probably the style sheet overrides the palette brushes.

      My currently best explanation is that it is not the QBrush in itself, but the setForeground method which takes precedence. I first thought that setForeground is a general QWidget method or something. This is not the case. If you have a look at the documentation of QTableWidgetItem::setForeground (https://doc.qt.io/qt-6/qtablewidgetitem.html#setForeground) it specifically states:

      Sets the item's foreground brush to the specified brush. Setting a default-constructed brush will let the view use the default color from the style.

      So: Yes, for a QTableWidgetItem setForeground overrides any previous style.

      Dummie1138D 1 Reply Last reply
      0
      • Christian EhrlicherC Offline
        Christian EhrlicherC Offline
        Christian Ehrlicher
        Lifetime Qt Champion
        wrote on last edited by
        #2

        As written in the documentation, style sheet always wins.

        Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
        Visit the Qt Academy at https://academy.qt.io/catalog

        Dummie1138D 1 Reply Last reply
        3
        • Christian EhrlicherC Christian Ehrlicher

          As written in the documentation, style sheet always wins.

          Dummie1138D Offline
          Dummie1138D Offline
          Dummie1138
          wrote on last edited by
          #3

          @Christian-Ehrlicher Thanks. In that case, what are some potential reasons that my QBrush seems to have taken priority? There are no other stylesheets taking priority over the QTable.

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

            I would have guessed the same as @Christian-Ehrlicher. Probably the style sheet overrides the palette brushes.

            My currently best explanation is that it is not the QBrush in itself, but the setForeground method which takes precedence. I first thought that setForeground is a general QWidget method or something. This is not the case. If you have a look at the documentation of QTableWidgetItem::setForeground (https://doc.qt.io/qt-6/qtablewidgetitem.html#setForeground) it specifically states:

            Sets the item's foreground brush to the specified brush. Setting a default-constructed brush will let the view use the default color from the style.

            So: Yes, for a QTableWidgetItem setForeground overrides any previous style.

            Dummie1138D 1 Reply Last reply
            0
            • S SimonSchroeder

              I would have guessed the same as @Christian-Ehrlicher. Probably the style sheet overrides the palette brushes.

              My currently best explanation is that it is not the QBrush in itself, but the setForeground method which takes precedence. I first thought that setForeground is a general QWidget method or something. This is not the case. If you have a look at the documentation of QTableWidgetItem::setForeground (https://doc.qt.io/qt-6/qtablewidgetitem.html#setForeground) it specifically states:

              Sets the item's foreground brush to the specified brush. Setting a default-constructed brush will let the view use the default color from the style.

              So: Yes, for a QTableWidgetItem setForeground overrides any previous style.

              Dummie1138D Offline
              Dummie1138D Offline
              Dummie1138
              wrote on last edited by
              #5

              @SimonSchroeder Thank you. In that case, I have two followup questions.
              1: Are there any other ways to change the color of a QTableWidgetItem's text without using setForeground? I have not yet been able to find any on the QTableWidgetItem documentation and I don't see any on the document.
              2: Are there ways of changing the stylesheets of individual rows/colums/cells? I'm assuming there aren't since I wasn't able to find any but I may be wrong.

              JonBJ 1 Reply Last reply
              0
              • Dummie1138D Dummie1138

                @SimonSchroeder Thank you. In that case, I have two followup questions.
                1: Are there any other ways to change the color of a QTableWidgetItem's text without using setForeground? I have not yet been able to find any on the QTableWidgetItem documentation and I don't see any on the document.
                2: Are there ways of changing the stylesheets of individual rows/colums/cells? I'm assuming there aren't since I wasn't able to find any but I may be wrong.

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

                @Dummie1138
                I feel there is some confusion here, which requires clarification. Qt stylesheet rules can only be applied to QWidgets, and QTableWidgetItem is not a QWidget. So it cannot be addressed via any stylesheet rule. QTableWidget is, of course, a QWidget, so that can be stylesheeted, and it draws the items. You cannot do any stylesheeting to individual items. You can use a QStyledItemDelegate for void QAbstractItemView::setItemDelegate(QAbstractItemDelegate *delegate) to control items' appearance.

                S 1 Reply Last reply
                0
                • JonBJ JonB

                  @Dummie1138
                  I feel there is some confusion here, which requires clarification. Qt stylesheet rules can only be applied to QWidgets, and QTableWidgetItem is not a QWidget. So it cannot be addressed via any stylesheet rule. QTableWidget is, of course, a QWidget, so that can be stylesheeted, and it draws the items. You cannot do any stylesheeting to individual items. You can use a QStyledItemDelegate for void QAbstractItemView::setItemDelegate(QAbstractItemDelegate *delegate) to control items' appearance.

                  S Offline
                  S Offline
                  SimonSchroeder
                  wrote on last edited by
                  #7

                  @JonB said in Color priority between stylesheets and QBrush.:

                  Qt stylesheet rules can only be applied to QWidgets, and QTableWidgetItem is not a QWidget. So it cannot be addressed via any stylesheet rule.

                  This is only partially correct. Yes, QTableWidgetItem is not a QWidget. However, when drawn Qt still applies a QWidget (or similar) internally. You can apply a stylesheet for QTableWidgetItems by addressing QTableWidget::item in the stylesheet. Though I am not aware that you can create a stylesheet per row/column/cell. This actually means you should use a QStyleItemDelegate as suggested.

                  @JonB Maybe I misread your statement and need to apologize. Probably a better wording is: "So it cannot be addressed individually via any stylesheet rule."

                  JonBJ 1 Reply Last reply
                  1
                  • S SimonSchroeder

                    @JonB said in Color priority between stylesheets and QBrush.:

                    Qt stylesheet rules can only be applied to QWidgets, and QTableWidgetItem is not a QWidget. So it cannot be addressed via any stylesheet rule.

                    This is only partially correct. Yes, QTableWidgetItem is not a QWidget. However, when drawn Qt still applies a QWidget (or similar) internally. You can apply a stylesheet for QTableWidgetItems by addressing QTableWidget::item in the stylesheet. Though I am not aware that you can create a stylesheet per row/column/cell. This actually means you should use a QStyleItemDelegate as suggested.

                    @JonB Maybe I misread your statement and need to apologize. Probably a better wording is: "So it cannot be addressed individually via any stylesheet rule."

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

                    @SimonSchroeder said in Color priority between stylesheets and QBrush.:

                    "So it cannot be addressed individually via any stylesheet rule."

                    Indeed. The OP was asking:

                    : Are there ways of changing the stylesheets of individual rows/colums/cells? I'm assuming there aren't since I wasn't able to find any but I may be wrong.

                    Each item is not a widget, cannot be individually addressed, cannot have, say, a dynamic property assigned to it so that you could alter it that way, etc.

                    ::item is documented as a "sub-control", with same standing as e.g. ::icon or ::tab.

                    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