Results 1 to 12 of 12

Thread: Color the rows in QTableView

  1. #1
    Join Date
    Jun 2009
    Location
    México
    Posts
    20
    Thanks
    7
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Color the rows in QTableView

    Hi! I'm using a QTableView and want to color the background of the rows, but i can't figure out how. THANKS FOR YOUR HELP!!

  2. #2
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Color the rows in QTableView

    take a look at Qt::BackgroundRole, i.e. you need to set data for a needed item,
    you can do it (in general way) as:
    Qt Code:
    1. ...
    2. QModelIndex index = model->index(0, 0);
    3. model->setData(index, Qt::red, Qt::BackgroundRole);
    4. ...
    To copy to clipboard, switch view to plain text mode 
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  3. #3
    Join Date
    Jan 2008
    Location
    Brasil
    Posts
    131
    Thanks
    18
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Color the rows in QTableView

    Hi, subclass the your Model (QSqlQuery for example) and reimplement virtual method data, like this:

    Qt Code:
    1. QVariant MyModelSqlQryModel::data(const QModelIndex &idx, int role) const
    2. {
    3.  
    4. QVariant v = QSqlQueryModel::data(idx, role);
    5.  
    6. if (role == Qt::BackgroundColor)
    7. {
    8. return QVariant(QColor(Qt::yellow));
    9. }
    10.  
    11. return (v);
    12. }
    To copy to clipboard, switch view to plain text mode 

    Marcelo E. Geyer

  4. The following user says thank you to estanisgeyer for this useful post:

    grub87 (17th June 2009)

  5. #4
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Color the rows in QTableView

    here is a compilable example
    Qt Code:
    1. #include <QtGui>
    2. #include <QApplication>
    3.  
    4. int main(int argc, char **argv)
    5. {
    6. QApplication app(argc, argv);
    7.  
    8. QTableWidget table(10, 10);
    9. QAbstractItemModel *model = table.model();
    10. for (int row = 0; row < table.rowCount(); ++row) {
    11. for (int column = 0; column < table.columnCount(); ++column) {
    12. QTableWidgetItem *newItem = new QTableWidgetItem(QObject::tr("%1").arg((row+1)*(column+1)));
    13. table.setItem(row, column, newItem);
    14. const QModelIndex index = model->index(row, column);
    15. model->setData(index, QColor(qrand()%255, qrand()%255, qrand()%255), Qt::BackgroundRole);
    16. }
    17. }
    18. table.show();
    19.  
    20. return app.exec();
    21. }
    To copy to clipboard, switch view to plain text mode 
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  6. #5
    Join Date
    Jun 2009
    Location
    México
    Posts
    20
    Thanks
    7
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Color the rows in QTableView

    Hi!! I try to put your code in a cycle, so it will color all the tableView, but it didn't work:

    Qt Code:
    1. int row = 0;
    2. if (query.isActive())
    3. while(query.next()){
    4. QModelIndex index = model->index(row, 1);
    5. model->setData(index, Qt::red, Qt::BackgroundRole);
    6. row++;
    7. }
    To copy to clipboard, switch view to plain text mode 

  7. #6
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Color the rows in QTableView

    if you use some of QSql*Model then you should approach which estanisgeyer suggested of write your own delegate.
    an exmaple
    Qt Code:
    1. class ColorDelegate: public QItemDelegate
    2. {
    3. public:
    4. ColorDelegate(QObject *parent = 0) : QItemDelegate(parent) {}
    5.  
    6. public:
    7. virtual void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
    8. {
    9. drawBackground(painter, option, index);
    10. QItemDelegate::paint(painter, option, index);
    11. }
    12.  
    13. protected:
    14. virtual void drawBackground(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
    15. {
    16. Q_UNUSED(index);
    17. painter->fillRect(option.rect, QColor(qrand()%255, qrand()%255, qrand()%255));
    18. }
    19. };
    To copy to clipboard, switch view to plain text mode 
    applying a delegate for a table
    Qt Code:
    1. ...
    2. table->setItemDelegate(new ColorDelegate(table));
    3. ...
    To copy to clipboard, switch view to plain text mode 
    but in this case when a table is being resized items colors is being changed to.
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  8. The following 2 users say thank you to spirit for this useful post:

    grub87 (17th June 2009), NicholasSmith (6th October 2010)

  9. #7
    Join Date
    Jun 2009
    Location
    México
    Posts
    20
    Thanks
    7
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Talking Re: Color the rows in QTableView

    Excelent!! thanks!! that get me out of trouble, and i'm also changing the colors of rows like this:

    Qt Code:
    1. ui.latabladiario->setItemDelegateForRow(0, new ColorDelegate(ui.latabladiario));
    2. ui.latabladiario->setItemDelegateForRow(3, new ColorDelegate(ui.latabladiario));
    To copy to clipboard, switch view to plain text mode 
    Last edited by grub87; 17th June 2009 at 17:07.

  10. #8
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Color the rows in QTableView

    add a map or hash to your delegate for keeping color by id (or even better add a proper method in a delegate in which you can pass this map/hash). then you have an index (QModelIndex) using it you can get your id from a table, this should look like this
    Qt Code:
    1. class ColorDelegate: public QItemDelegate
    2. {
    3. public:
    4. ColorDelegate(QObject *parent = 0) : QItemDelegate(parent) {}
    5.  
    6. public:
    7. virtual void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
    8. {
    9. drawBackground(painter, option, index);
    10. QItemDelegate::paint(painter, option, index);
    11. }
    12.  
    13. void setColorMap(const QMap<int, QColor> &colorMap) { m_colorMap = colorMap; }
    14.  
    15. protected:
    16. virtual void drawBackground(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
    17. {
    18. if (index.column() == 1) {//assume that id keeps in a second column
    19. const int id = index.data().toInt();
    20. painter->fillRect(option.rect, m_colorMap.value(id));
    21. }
    22. }
    23.  
    24. private:
    25. QMap<int, QColor> m_colorMap;
    26. };
    To copy to clipboard, switch view to plain text mode 
    usage
    Qt Code:
    1. ...
    2. ColorDelegate *delegate = new ColorDelegate(&table);
    3. QMap<int, QColor> map;
    4. for (int i = 0; i < 100; ++i)
    5. map.insert(i, QColor(qrand()%255, qrand()%255, qrand()%255));
    6. delegate->setColorMap(map);
    7. table->setItemDelegate(delegate);
    8. ...
    To copy to clipboard, switch view to plain text mode 
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  11. #9
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Color the rows in QTableView

    Quote Originally Posted by grub87 View Post
    Excelent!! thanks!! that get me out of trouble, and i'm also changing the colors of rows like this:

    Qt Code:
    1. ui.latabladiario->setItemDelegateForRow(0, new ColorDelegate(ui.latabladiario));
    2. ui.latabladiario->setItemDelegateForRow(3, new ColorDelegate(ui.latabladiario));
    To copy to clipboard, switch view to plain text mode 
    yup, or like this
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  12. #10
    Join Date
    Jan 2008
    Location
    Brasil
    Posts
    131
    Thanks
    18
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Lightbulb Re: Color the rows in QTableView

    There are several ways to do this, see the example below, if the date is less than the current, applying background color of red.

    Example:
    Qt Code:
    1. QVariant MyModel::data(const QModelIndex &idx, int role) const
    2. {
    3. QVariant v = QSqlQueryModel::data(idx, role);
    4.  
    5. if ((role == Qt::BackgroundRole) &&
    6. (index(idx.row(), 0, idx.parent()).data().toDate() < QDate::currentDate()))
    7. {
    8. return QVariant(QColor(255, 0, 0));
    9. }
    10.  
    11. return (v);
    12. }
    To copy to clipboard, switch view to plain text mode 

    Marcelo E. Geyer

  13. The following user says thank you to estanisgeyer for this useful post:

    grub87 (17th June 2009)

  14. #11
    Join Date
    Jun 2009
    Location
    México
    Posts
    20
    Thanks
    7
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Talking Re: Color the rows in QTableView

    This is my final code, i only put in a for cycle to move along the rows:

    Qt Code:
    1. int polizaInicial = 1;
    2. for (int row = 0; row < numRows; row++) {
    3. const QModelIndex index = model->index(row, 1);
    4. if(index.data().toInt() == polizaInicial){
    5. ui.latabladiario->setItemDelegateForRow(row, new ColorDelegate(ui.latabladiario));
    6. }
    7. else{
    8. polizaInicial++;
    9. }
    To copy to clipboard, switch view to plain text mode 

    THANKS!!

  15. #12
    Join Date
    Apr 2006
    Location
    Denmark / Norway
    Posts
    67
    Thanks
    3
    Thanked 12 Times in 8 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Color the rows in QTableView

    You can also use style sheets for selection:
    http://doc.trolltech.com/4.5/stylesh...ing-qtableview

    I think background color and color should work as well, but that needs to be tested.

Similar Threads

  1. Set height of QTableView to fit exact number of rows.
    By Ben.Hines in forum Qt Programming
    Replies: 3
    Last Post: 17th January 2019, 01:49
  2. Remove selected rows from a QTableView
    By niko in forum Qt Programming
    Replies: 4
    Last Post: 3rd March 2016, 12:49
  3. Count the number of rows in a QTableView
    By grub87 in forum Qt Programming
    Replies: 9
    Last Post: 28th June 2009, 16:31
  4. QTableView has constant number of rows
    By tomeks in forum Qt Programming
    Replies: 5
    Last Post: 10th December 2008, 15:29
  5. QTableView number of rows and scrollbar issue
    By jnk5y in forum Qt Programming
    Replies: 3
    Last Post: 1st March 2006, 06:55

Tags for this Thread

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.