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. Setting background color to a whole table column including header item

Setting background color to a whole table column including header item

Scheduled Pinned Locked Moved General and Desktop
5 Posts 2 Posters 2.2k 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.
  • B Offline
    B Offline
    Binary91
    wrote on 26 Oct 2014, 09:38 last edited by
    #1

    Hi,
    I've got a QListWidget that contains the horizontal header contents of a QTableWidget.
    When user selects (clicks) on one of those items in the QListWidget, I'd like to enhance the whole column that has the clicked header item.
    My idea was to do it in a loop and set a background-color to every table cell, but there are two problems:

    1. I don't know how to enhance the header item
    2. each table cell contains a full-sized QWidget with a checkbox within. So what I need to do is setting a background-color to the QWidget within, right? How can I do that? Also I would have to make the checkbox text transparent, so there is no white container around it...

    Does anyone have a solution for that?

    1 Reply Last reply
    0
    • B Offline
      B Offline
      Binary91
      wrote on 26 Oct 2014, 17:41 last edited by
      #2

      Well, my first try was to add QTableWidgetItems to the horizontal header and then using setBackground:
      @for(int i = 0; i < this->table->columnCount(); i++)
      {
      QTableWidgetItem* item = new QTableWidgetItem();
      item->setBackground(QBrush(QColor(255,0,0)));
      item->setForeground(QBrush(QColor(0,255,0)));
      item->setText("Column "+QString::number(i));
      this->table->setHorizontalHeaderItem(i, item);
      }@
      The interesting thing is, setting text color via setForeground(..) works well to each header item, but setting background color via setBackground(..) has no effect... why?

      1 Reply Last reply
      0
      • V Offline
        V Offline
        Vinorcola
        wrote on 26 Oct 2014, 23:58 last edited by
        #3

        Hi,

        To do that, I would have create a method in the model called something like setCurrentColumn(const QModelIndex& index) and a private int called, let's say m_currentColumn.

        Make sure in your data() and headerData() method that you take m_currentColumn in account to render a different background color (using BackgroundRole).

        Now, in your method setCurrentColumn(), you emit the dataChanged signal to indicate the BackgroundRole change for all the row of the column m_currentColumn. Do the same with headerDataChanged().
        Then, update m_currentColumn to the new column (using the index pass as argument) and emit again the signal dataChanged() and headerDataChanged() using the new value of m_currentColumn.

        By the way, do you known the model handle by itself checkable elements? See Qt::ItemIsUserCheckable flag.

        Hope it helps.

        1 Reply Last reply
        0
        • B Offline
          B Offline
          Binary91
          wrote on 27 Oct 2014, 10:47 last edited by
          #4

          Hm, sorry but I do not understand how you set a specific background color via setBackgroundRole(QPallete::ColorRole); ... When I take a look at the documentation, ColorRoles are stuff like "Window", "WindowText", "Base" etc... but I'm looking for something to define the specific color like QColor(255,0,0)... how do I manage this?

          1 Reply Last reply
          0
          • V Offline
            V Offline
            Vinorcola
            wrote on 27 Oct 2014, 18:55 last edited by
            #5

            @
            QVariant data(const QModelIndex& index, int role)
            {
            if (index.isValid() && index.row() < rowCount())
            {
            // ...

                if (index.column() == m_currentColumn && role == Qt::BackgroundColor)
                {
                    return QColor(255, 0, 0);
                }
            }
            

            }
            @

            1 Reply Last reply
            0

            5/5

            27 Oct 2014, 18:55

            • Login

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