PDA

View Full Version : HeaderView BackgroundColor



dragon
8th July 2008, 17:31
Hello anyone,

Iám using Qt 4.3.2 on Windows XP.
In my MainWindow i have a TableView this TableView i use for showing a database.
I have set myTableView to a model.
Everything works perfect i see the database the columns and rows en de headers.
I have subclass the SQLTableModel into CustomSqlModel because to change the background for the collumns in different colors works also perfect. (BackgroundColorRole)
My question is it possible to change the backgroundcolor of the headers im my database.
I have tried to do this in the CustomSqlModel with no success or must i use itemdelegate.
I have also tried with stylesheet no success.

Thanks in advance.

caduel
8th July 2008, 17:39
yes, you can change the header's backgroud.
Just return BackgroundRole (in headerData()) with the appropriate value.


Do you want to read the background color from a db table? (Did I get that right?)
That is possible, too.

dragon
8th July 2008, 18:44
I have tried also headerdata see example:

in h file
Qvariant headerdata(int section, Qt::orientation orientation, int role = Qt:: BackgroundColorRole) const;

In cpp file.

QVariant MainWindow::headerdata(int section, QT::orientation, int role) const
{
if (role == BackgroundColorRole)
{
return (QColor(Qt::yellow));
}
return QVariant();
}

This was with no success.

No i'dont want to read backgroundcolor from a db table.

Thanks in advance

wysota
8th July 2008, 19:10
I'd say the problem is with the widget style you are using. I would guess that WindowsXP style doesn't allow one to set a custom background colour for the header. Try running your application with a -style plastique switch and see if the colour of the table is like you want it.

dragon
8th July 2008, 19:45
I also did -style plastique with my application.
No success.

I now sure that is not possible in Windows XP.
I will try my application in my Linux distrubution.
Thanks caduel and wysota for suggestions and time.

wysota
8th July 2008, 20:09
If it doesn't work with -style plastique then it won't work on Linux either as it uses the plastique style by default. I didn't say it is not possible with Windows XP but with "WindowsXP style". Did the looks of the application change when you started it with -style plastique?

dragon
8th July 2008, 22:02
Yes the application change a little bit with -style plastque.

wysota
8th July 2008, 22:05
Hmm... a little bit? :) BTW. What does your "MainWindow" class inherit from? If not from QAbstractItemView or one of its subclasses then that's why your header doesn't change colour ;)

caduel
9th July 2008, 07:02
try passing a QBrush instead of a QColor?
(and use Qt::BackgroundRole, Qt::BackgroudColorRole is obsolete)

HTH

dragon
9th July 2008, 17:39
My "MainWindow" class inherit from QMainWindow see code snippet.


#include <QtGui>
#include <QtOpenGL>
#include <QtSql>

#include "mainwindow.h"
#include "glwidget.h"
#include "customsqlmodel.h" //CustomSqlModel

MainWindow::MainWindow(QWidget *parent)
:QMainWindow(parent)
{

setupUi(this);

setWindowIcon(QIcon(":/images/triple20.png"));
connect(actionInfo, SIGNAL(triggerred()), this, SLOT(info()));
connect(actionTriple_S, SIGNAL(triggered()), this, SLOT(triple()));
connect(actionAfsluiten, SIGNAL(triggered()), this, SLOT(exit()));
connect(pbVerzend, SIGNAL(clicked()), this, SLOT(verzend()));
connect(pbVerbind, SIGNAL(clicked()), this, SLOT(verbind()));

model = new CustomSqlModel(this);
model->setTable("planning");
model->setEditStrategy(QSqlTableModel::OnManualSubmit);
model->select();

model->setHeaderData(0, Qt::Horizontal, tr("Categorie")); //HeaderLabels
model->setHeaderData(1, Qt::Horizontal, tr("Klant"));
model->setHeaderData(2, Qt::Horizontal, tr("Plaats"));
model->setHeaderData(3, Qt::Horizontal, tr("Geschat pot"));
model->setHeaderData(4, Qt::Horizontal, tr("Bezocht"));
model->setHeaderData(5, Qt::Horizontal, tr("Planning"));

view->setModel(model);

// etc....

}


My CustomSqlModel see code snippet h file.


#ifndef CUSTOMSQLMODEL_H
#define CUSTOMSQLMODEL_H

#include <QSqlTableModel>
#include <QVariant>
#include <QModelIndex>

class CustomSqlModel : public QSqlTableModel
{
Q_OBJECT

public:
CustomSqlModel(QObject *parent = 0);

QVariant data(const QModelIndex &item, int role) const;
QVariant headerdata(int section, Qt::Orientation orientation, int role = Qt::BackgroundColorRole) const;
//int rowCount(const QModelIndex &parent = QModelIndex()) const;
};

#endif



My CustomSqlModel see code snippet.cpp file


#include <QtGui>

#include "customsqlmodel.h"

CustomSqlModel::CustomSqlModel(QObject *parent)
: QSqlTableModel(parent)
{
}

QVariant CustomSqlModel::data(const QModelIndex &index, int role) const
{
QVariant val0 = QSqlTableModel::data(index, role);
if (role == Qt::BackgroundColorRole && index.column() == 0)
{
return qVariantFromValue(QColor(Qt::gray));
}

// etc....

return val0;

}

QVariant CustomSqlModel::headerdata(int section, Qt::Orientation orientation, int role) const
{
if(role == Qt::BackgroundColorRole)
{
return (QColor(Qt::yellow));
}
return QVariant();

}

caduel
9th July 2008, 19:22
yes, and...
what is your question?

did you try returning a QBrush instead of a QColor?