Results 1 to 11 of 11

Thread: HeaderView BackgroundColor

  1. #1
    Join Date
    Jan 2006
    Posts
    115
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default HeaderView BackgroundColor

    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.

  2. #2
    Join Date
    Dec 2006
    Posts
    849
    Thanks
    6
    Thanked 163 Times in 151 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: HeaderView BackgroundColor

    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.

  3. #3
    Join Date
    Jan 2006
    Posts
    115
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: HeaderView BackgroundColor

    I have tried also headerdata see example:

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

    In cpp file.
    Qt Code:
    1. QVariant MainWindow::headerdata(int section, QT::orientation, int role) const
    2. {
    3. if (role == BackgroundColorRole)
    4. {
    5. return (QColor(Qt::yellow));
    6. }
    7. return QVariant();
    8. }
    To copy to clipboard, switch view to plain text mode 
    This was with no success.

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

    Thanks in advance
    Last edited by jpn; 8th July 2008 at 21:13. Reason: missing [code] tags

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: HeaderView BackgroundColor

    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.

  5. #5
    Join Date
    Jan 2006
    Posts
    115
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: HeaderView BackgroundColor

    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.

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: HeaderView BackgroundColor

    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?

  7. #7
    Join Date
    Jan 2006
    Posts
    115
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: HeaderView BackgroundColor

    Yes the application change a little bit with -style plastque.

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: HeaderView BackgroundColor

    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

  9. #9
    Join Date
    Dec 2006
    Posts
    849
    Thanks
    6
    Thanked 163 Times in 151 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: HeaderView BackgroundColor

    try passing a QBrush instead of a QColor?
    (and use Qt::BackgroundRole, Qt::BackgroudColorRole is obsolete)

    HTH

  10. #10
    Join Date
    Jan 2006
    Posts
    115
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: HeaderView BackgroundColor

    My "MainWindow" class inherit from QMainWindow see code snippet.
    Qt Code:
    1. #include <QtGui>
    2. #include <QtOpenGL>
    3. #include <QtSql>
    4.  
    5. #include "mainwindow.h"
    6. #include "glwidget.h"
    7. #include "customsqlmodel.h" //CustomSqlModel
    8.  
    9. MainWindow::MainWindow(QWidget *parent)
    10. :QMainWindow(parent)
    11. {
    12.  
    13. setupUi(this);
    14.  
    15. setWindowIcon(QIcon(":/images/triple20.png"));
    16. connect(actionInfo, SIGNAL(triggerred()), this, SLOT(info()));
    17. connect(actionTriple_S, SIGNAL(triggered()), this, SLOT(triple()));
    18. connect(actionAfsluiten, SIGNAL(triggered()), this, SLOT(exit()));
    19. connect(pbVerzend, SIGNAL(clicked()), this, SLOT(verzend()));
    20. connect(pbVerbind, SIGNAL(clicked()), this, SLOT(verbind()));
    21.  
    22. model = new CustomSqlModel(this);
    23. model->setTable("planning");
    24. model->setEditStrategy(QSqlTableModel::OnManualSubmit);
    25. model->select();
    26.  
    27. model->setHeaderData(0, Qt::Horizontal, tr("Categorie")); //HeaderLabels
    28. model->setHeaderData(1, Qt::Horizontal, tr("Klant"));
    29. model->setHeaderData(2, Qt::Horizontal, tr("Plaats"));
    30. model->setHeaderData(3, Qt::Horizontal, tr("Geschat pot"));
    31. model->setHeaderData(4, Qt::Horizontal, tr("Bezocht"));
    32. model->setHeaderData(5, Qt::Horizontal, tr("Planning"));
    33.  
    34. view->setModel(model);
    35.  
    36. // etc....
    37.  
    38. }
    To copy to clipboard, switch view to plain text mode 
    My CustomSqlModel see code snippet h file.
    Qt Code:
    1. #ifndef CUSTOMSQLMODEL_H
    2. #define CUSTOMSQLMODEL_H
    3.  
    4. #include <QSqlTableModel>
    5. #include <QVariant>
    6. #include <QModelIndex>
    7.  
    8. class CustomSqlModel : public QSqlTableModel
    9. {
    10. Q_OBJECT
    11.  
    12. public:
    13. CustomSqlModel(QObject *parent = 0);
    14.  
    15. QVariant data(const QModelIndex &item, int role) const;
    16. QVariant headerdata(int section, Qt::Orientation orientation, int role = Qt::BackgroundColorRole) const;
    17. //int rowCount(const QModelIndex &parent = QModelIndex()) const;
    18. };
    19.  
    20. #endif
    To copy to clipboard, switch view to plain text mode 

    My CustomSqlModel see code snippet.cpp file
    Qt Code:
    1. #include <QtGui>
    2.  
    3. #include "customsqlmodel.h"
    4.  
    5. CustomSqlModel::CustomSqlModel(QObject *parent)
    6. : QSqlTableModel(parent)
    7. {
    8. }
    9.  
    10. QVariant CustomSqlModel::data(const QModelIndex &index, int role) const
    11. {
    12. QVariant val0 = QSqlTableModel::data(index, role);
    13. if (role == Qt::BackgroundColorRole && index.column() == 0)
    14. {
    15. return qVariantFromValue(QColor(Qt::gray));
    16. }
    17.  
    18. // etc....
    19.  
    20. return val0;
    21.  
    22. }
    23.  
    24. QVariant CustomSqlModel::headerdata(int section, Qt::Orientation orientation, int role) const
    25. {
    26. if(role == Qt::BackgroundColorRole)
    27. {
    28. return (QColor(Qt::yellow));
    29. }
    30. return QVariant();
    31.  
    32. }
    To copy to clipboard, switch view to plain text mode 

  11. #11
    Join Date
    Dec 2006
    Posts
    849
    Thanks
    6
    Thanked 163 Times in 151 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: HeaderView BackgroundColor

    yes, and...
    what is your question?

    did you try returning a QBrush instead of a QColor?

Similar Threads

  1. Replies: 1
    Last Post: 17th March 2008, 15:02
  2. Backgroundcolor QLCDNumber in Windows?
    By Teuniz in forum Qt Programming
    Replies: 4
    Last Post: 30th August 2006, 16:09
  3. [ItemView] Bug in headerview
    By lauranger in forum Qt Programming
    Replies: 3
    Last Post: 14th August 2006, 12:55
  4. QTreeWidget backgroundcolor
    By vmferreira in forum Qt Programming
    Replies: 5
    Last Post: 9th August 2006, 21:21

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.