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 of HeadData for QTableView is doesn't work
Forum Updated to NodeBB v4.3 + New Features

Setting background of HeadData for QTableView is doesn't work

Scheduled Pinned Locked Moved General and Desktop
15 Posts 5 Posters 7.5k 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.
  • C Offline
    C Offline
    csding
    wrote on last edited by
    #1

    @
    QVariant TableModel::headerData(int section, Qt::Orientation orientation, int role) const
    {
    if(role ==Qt::BackgroundRole)
    {
    //if(orientation == Qt::Vertical)
    if (orientation == Qt::Horizontal)
    switch (section)
    {
    case 0:
    return QBrush(QColor(Qt::blue));
    case 1:
    return QVariant(QColor(Qt::blue));
    default:
    return QAbstractTableModel::headerData(section,orientation,role);
    }
    }
    }
    @
    code of part:
    @
    class TableModel : public QAbstractTableModel
    {
    Q_OBJECT
    public:
    TableModel(int Columns, QStringList Names, QObject *parent = 0);
    int rowCount(const QModelIndex &parent) const;
    int columnCount(const QModelIndex &parent) const;
    QVariant data(const QModelIndex &index, int role) const;
    QVariant headerData(int section, Qt::Orientation orientation, int role) const;
    }
    @
    @
    class SortFilterProxyModel : public QSortFilterProxyModel
    {
    Q_OBJECT

    public:
    SortFilterProxyModel(QString typefile, QObject *parent = 0);
    QVariant data(const QModelIndex &index, int role) const;
    QVariant headerData(int section, Qt::Orientation orientation, int role) const;
    }
    @
    @
    int main(int argc, char *argv[])
    {
    SortFilterProxyModel * proxyModel = new SortFilterProxyModel();
    proxyModel->setSourceModel(table);
    QTableView * tableView->new QTableView();
    tableView->setModel(proxyModel);
    return a.exec();
    }
    @

    Above code is not setting background in my program.

    Setting color or style of fonts of HeadData is working, but background of HeadData is not work in function QVariant TableModel::headerData(int section, Qt::Orientation orientation, int role) const using return QVariant(QColor(Qt::blue)); or return QBrush(QColor(Qt::blue));
    Function setStyleSheet() is working normally, but it is not metting my dynamic setting color on any rows or colums with HeadData.
    @
    tableView->horizontalHeader()->setStyleSheet("QHeaderView::section{background:skyblue;}");
    @
    Above the code is working, But i cannot use it to metting my goal. I want "return QVariant(QColor(Qt::blue));" to work for my goal.
    Can anyone help me?
    Thanks!

    1 Reply Last reply
    0
    • C Offline
      C Offline
      csding
      wrote on last edited by
      #2

      Sofa is mine.

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

        try using background-color. At least in the "docs":http://qt-project.org/doc/qt-4.8/stylesheet-examples.html#customizing-qheaderview they use it??

        --- 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
        • C Offline
          C Offline
          csding
          wrote on last edited by
          #4

          This code isworking.
          tableView->horizontalHeader()->setStyleSheet("QHeaderView::section{background:skyblue;}");
          But it is not metting my dynamic setting color on any rows or colums with HeadData.
          [quote author="raven-worx" date="1371192413"]try using background-color. At least in the "docs":http://qt-project.org/doc/qt-4.8/stylesheet-examples.html#customizing-qheaderview they use it??[/quote]

          1 Reply Last reply
          0
          • A Offline
            A Offline
            andre
            wrote on last edited by
            #5

            According to the documentation, using the background role should work. I see no obvious mistake in your code from your first post. Please prepare a small test case to demonstrate the issue in isolation. First post, so we can have a look. Then, if it turns out to be a bug, file a bug report.

            1 Reply Last reply
            0
            • C Offline
              C Offline
              csding
              wrote on last edited by
              #6

              OK.
              [quote author="Andre" date="1371196833"]According to the documentation, using the background role should work. I see no obvious mistake in your code from your first post. Please prepare a small test case to demonstrate the issue in isolation. First post, so we can have a look. Then, if it turns out to be a bug, file a bug report.[/quote]

              1 Reply Last reply
              0
              • C Offline
                C Offline
                csding
                wrote on last edited by
                #7

                U p!

                1 Reply Last reply
                0
                • C Offline
                  C Offline
                  csding
                  wrote on last edited by
                  #8

                  TestBackground.pro
                  @
                  QT += core gui

                  greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

                  TARGET = TestBackground
                  TEMPLATE = app

                  SOURCES += main.cpp
                  mainwindow.cpp
                  tablemodel.cpp

                  HEADERS += mainwindow.h
                  tablemodel.h
                  @

                  tablemodel.h
                  @
                  #ifndef TABLEMODEL_H
                  #define TABLEMODEL_H
                  #include <QAbstractTableModel>

                  class TableModel : public QAbstractTableModel
                  {
                  Q_OBJECT
                  public:
                  TableModel(QObject *parent = 0);
                  int rowCount(const QModelIndex &parent) const;
                  int columnCount(const QModelIndex &parent) const;
                  QVariant data(const QModelIndex &index, int role) const;
                  QVariant headerData(int section, Qt::Orientation orientation, int role) const;
                  private:
                  };
                  #endif

                  @

                  tablemodel.cpp
                  @
                  #include "tablemodel.h"
                  #include <QColor>
                  #include <QBrush>
                  #include <qDebug>
                  #include <QString>

                  TableModel::TableModel(QObject *parent)
                  : QAbstractTableModel(parent)
                  {
                  }

                  int TableModel::rowCount(const QModelIndex &parent) const
                  {
                  Q_UNUSED(parent);
                  return 2;
                  }

                  int TableModel::columnCount(const QModelIndex &parent) const
                  {
                  Q_UNUSED(parent);
                  return 2;
                  }

                  QVariant TableModel::data(const QModelIndex &index, int role) const
                  {
                  if (!index.isValid())
                  return QVariant();

                  if (role == Qt::DisplayRole)
                  {
                      if (index.column() == 0)
                          return QString("zhang san");
                      else if (index.column() == 1)
                          return QString("china");
                  }
                  if(role == Qt::BackgroundRole)
                  {
                      if (index.column() == 0)
                          return QVariant(QColor(Qt::green));
                      else if (index.column() == 1)
                          return QVariant(QColor(Qt::green));
                  }
                  
                  return QVariant();
                  

                  }

                  QVariant TableModel::headerData(int section, Qt::Orientation orientation, int role) const
                  {
                  if (role == Qt::DisplayRole)
                  {
                  if (orientation == Qt::Horizontal) {
                  switch (section) {
                  case 0:
                  return tr("Name");
                  case 1:
                  return tr("Address");

                              default:
                                  return QVariant();
                          }
                      }
                  }
                  if(role == Qt::ForegroundRole)
                  {
                      if (orientation == Qt::Horizontal)
                      {
                          switch (section)
                          {
                              case 0:
                                  return QVariant(QColor(Qt::blue));
                              case 1:
                                  return QVariant(QColor(Qt::blue));
                              default:
                                  return QAbstractTableModel::headerData(section,orientation,role);
                          }
                      }
                  }
                  if(role ==Qt::BackgroundRole)
                  {
                      //if(orientation == Qt::Vertical)
                      if (orientation == Qt::Horizontal)
                      {
                          switch (section)
                          {
                              case 0:
                                  qDebug() << "case 0";
                                  return  QBrush(QColor(Qt::red));
                              case 1:
                                  qDebug() << "case 1";
                                  return  QVariant(QColor(Qt::red));
                              default:
                                  qDebug() << "default";
                                  return QAbstractTableModel::headerData(section,orientation,role);
                          }
                      }
                  }
                  
                  return QVariant();
                  

                  }
                  @

                  mainwindow.h
                  @
                  #ifndef MAINWINDOW_H
                  #define MAINWINDOW_H
                  #include <QMainWindow>
                  #include <QTableView>
                  #include <tablemodel.h>
                  class MainWindow : public QMainWindow
                  {
                  Q_OBJECT
                  public:
                  explicit MainWindow(QWidget *parent = 0);
                  ~MainWindow();
                  private:
                  TableModel * table;
                  };
                  #endif
                  @

                  mainwindow.cpp
                  @
                  #include "mainwindow.h"

                  MainWindow::MainWindow(QWidget *parent) :
                  QMainWindow(parent)
                  {
                  table = new TableModel(this);
                  QTableView *tableView = new QTableView;
                  tableView->setModel(table);
                  setCentralWidget(tableView);
                  }
                  MainWindow::~MainWindow()
                  {
                  }
                  @

                  main.cpp
                  @
                  #include "mainwindow.h"
                  #include <QApplication>

                  int main(int argc, char *argv[])
                  {
                  QApplication a(argc, argv);
                  MainWindow w;
                  w.show();
                  return a.exec();
                  }
                  @

                  Setting background of HeadData for QTableView is doesn’t work. Is it a bug for Qt ?
                  My original post is here:
                  http://qt-project.org/forums/viewthread/28840/#129491

                  1 Reply Last reply
                  0
                  • T Offline
                    T Offline
                    tobias.hunger
                    wrote on last edited by
                    #9

                    Please "file a bug report":https://bugreports.qt-project.org/ for things you consider to be a bug.

                    The bugtracker is a way better tool to discuss (possible) bugs than this forum.

                    1 Reply Last reply
                    0
                    • A Offline
                      A Offline
                      andre
                      wrote on last edited by
                      #10

                      [quote author="csding" date="1371345306"]U p![/quote]

                      Well... we agreed you'd create a small, self-contained, compile-able example demonstrating the issue. Based on that, I said we could take a look to see if the error is on your side after all, or you have found a bug in Qt. So far, I haven't seen anything of the sort.

                      1 Reply Last reply
                      0
                      • C Offline
                        C Offline
                        csding
                        wrote on last edited by
                        #11

                        I have posted the small,self-contained, compile-able example in a new another thread. You can visit on http://qt-project.org/forums/viewthread/28901/
                        Sorry, maybe i should notice you with it intime.

                        [quote author="Andre" date="1371451763"]
                        [quote author="csding" date="1371345306"]U p![/quote]

                        Well... we agreed you'd create a small, self-contained, compile-able example demonstrating the issue. Based on that, I said we could take a look to see if the error is on your side after all, or you have found a bug in Qt. So far, I haven't seen anything of the sort.[/quote]

                        1 Reply Last reply
                        0
                        • A Offline
                          A Offline
                          andre
                          wrote on last edited by
                          #12

                          I merged the two topics. Please don't start more than one topic on the same issue. Tobias: I asked for the example as a form of bug triaging even before it ends up in the tracker :)

                          1 Reply Last reply
                          0
                          • C Offline
                            C Offline
                            csding
                            wrote on last edited by
                            #13

                            First i want to know whether it is a bug in Qt, if true, i can write a bug report hand in. Waitting you testing for it.
                            thanks!.

                            [quote author="Andre" date="1371454106"]I merged the two topics. Please don't start more than one topic on the same issue. Tobias: I asked for the example as a form of bug triaging even before it ends up in the tracker :)

                            [/quote]

                            1 Reply Last reply
                            0
                            • C Offline
                              C Offline
                              csding
                              wrote on last edited by
                              #14

                              Please download the small case visit on:
                              http://pan.baidu.com/share/link?shareid=3014503980&uk=3070491333

                              1 Reply Last reply
                              0
                              • B Offline
                                B Offline
                                BenShafer
                                wrote on last edited by
                                #15

                                Is there a bug to track for this item yet? I have the same problem, but I'm still using 4.7.2. Has it been fixed in 4.8.6?

                                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