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. QHeaderView vertical headers do not change background color
Forum Updated to NodeBB v4.3 + New Features

QHeaderView vertical headers do not change background color

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 3 Posters 621 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.
  • D Offline
    D Offline
    dheerajshenoy
    wrote on last edited by
    #1

    I am trying to change the background color of QHeaderView using the following code:

    QVariant FileSystemModel::headerData(int section, Qt::Orientation orientation,
                                         int role) const {
        if (orientation == Qt::Vertical) {
            switch (role) {
    
            case static_cast<int>(Role::Marked):
                return m_markedFiles.contains(getPathFromRow(section));
                break;
    
            case Qt::BackgroundRole: {
              // QColor color;
              // bool isMarked = m_markedFiles.contains(getPathFromRow(section));
              // if (isMarked)
              //     return color.fromString("#FF5000");
              // return color;
              // TODO: Fix this
            } break;
    
            case Qt::FontRole: {
                QFont font;
                bool isMarked = m_markedFiles.contains(getPathFromRow(section));
                if (isMarked) {
                    font.setBold(true);
                    font.setItalic(true);
                    return font;
                }
                return font;
            }
    
            case Qt::DisplayRole:
                return section;
                break;
    
            default:
                return QVariant();
                break;
    
            }
    
    
    
        } else if (orientation == Qt::Horizontal && role == Qt::DisplayRole) {
    
            switch (m_column_list.at(section)) {
            case ColumnType::FileName:
                return "Name";
            case ColumnType::FileSize:
                return "Size";
            case ColumnType::FileModifiedDate:
                return "Last Modified";
            case ColumnType::FilePermission:
                return "Permissions";
            default:
                return QVariant();
            }
        }
    
        return QVariant();
    }
    

    I did the same thing with the rows of QTableView and it worked. But for some reason, the header view doesn't respect it. The font changes, but the foreground and background roles do not.

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

      I would guess the style ignores it. E.g. the windows style does not allow modifying the header color but fusion does iirc.

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

      1 Reply Last reply
      1
      • D Offline
        D Offline
        dheerajshenoy
        wrote on last edited by
        #3

        So, there is no way to override the defaults ?

        SGaistS 1 Reply Last reply
        0
        • D dheerajshenoy

          So, there is no way to override the defaults ?

          SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @dheerajshenoy create a subclass of QHeaderView, QProxyStyle or use the fusion style.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • D Offline
            D Offline
            dheerajshenoy
            wrote on last edited by
            #5

            I created a subclass of QProxyStyle and implemented the drawControl function. It still does not respect the headerData backgroundRole and foregroundRole

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              Not knowing how you implemented it does not help finding where the issue is.

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              0
              • D Offline
                D Offline
                dheerajshenoy
                wrote on last edited by
                #7

                To solve this, I ended up creating a subclass of the QHeaderView and added my background color logic (background using the background role of the underlying model associated) inside thepaintSection method.

                1 Reply Last reply
                0
                • D dheerajshenoy has marked this topic as solved on

                • Login

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