Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Setting background of HeadData for QTableView is doesn't work



  • @
    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!



  • Sofa is mine.


  • Moderators

    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??



  • 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]



  • 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.



  • 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]



  • U p!



  • 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


  • Moderators

    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.



  • [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.



  • 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]



  • 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 :)



  • 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]



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



  • 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?


Log in to reply