Results 1 to 9 of 9

Thread: Color table row with ItemDelegate

  1. #1
    Join Date
    Jun 2006
    Posts
    9
    Thanks
    2
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11

    Default Color table row with ItemDelegate

    Hello, i want to color a whole row using a custom delegate when a specific column contains a date bigger then a set date. What i can actually accomplish is just setting the background color of the cell ...


    Atm the code looks like this:

    Qt Code:
    1. void Delegate::paint(QPainter *painter,
    2. const QStyleOptionViewItem &option,
    3. const QModelIndex &index) const
    4. {
    5. if ( index.column() == 11 )
    6. {
    7. QDateTime compareDate = QDateTime::fromString("2010-01-01T23:59:59", Qt::ISODate);
    8.  
    9. QVariant date = index.model()->data(index, Qt::DisplayRole);
    10. QDateTime dt = date.toDateTime();
    11. if ( dt > compareDate )
    12. {
    13. if (option.state & QStyle::State_Enabled)
    14. painter->fillRect(option.rect, Qt::green);
    15. QItemDelegate::paint(painter, option, index);
    16. }
    17. }
    18. QItemDelegate::paint(painter, option, index);
    19. }
    To copy to clipboard, switch view to plain text mode 

    can anybody help with row coloring?

  2. #2
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Color table row with ItemDelegate

    You don't need a delegate to do that. Model items have a specific data role for setting the background color used for items rendered with the default delegate.

    You could do the same for example in model's setData():
    Qt Code:
    1. bool YourModel::setData(const QModelIndex& index, const QVariant& value, int role)
    2. {
    3. if (index.column() == 11 && role == Qt::EditRole)
    4. {
    5. // ...
    6. if (dt > compareDate)
    7. {
    8. // set the background color to all siblings (includes the index itself..)
    9. for (int col = 0; col < columnCount(index.parent()); ++col)
    10. {
    11. QModelIndex sibling = index.sibling(index.row(), col);
    12. setData(sibling, QColor(Qt::green), Qt::BackgroundColorRole);
    13. }
    14. }
    15. }
    16. return BaseClassModel::setData(index, value, role);
    17. }
    To copy to clipboard, switch view to plain text mode 
    J-P Nurmi

  3. #3
    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: Color table row with ItemDelegate

    I would simplify it even more:

    Qt Code:
    1. QVariant YourModel::data(const QModelIndex& ind, int role){
    2. //...
    3. if(role==Qt::BackgroundColorRole){
    4. QModelIndex testindex = index(ind.row(), 7, ind.parent()); // get index of "watched" column
    5. if(ind.column()==7 || data(testindex, Qt::DisplayRole).toDate() > ...)
    6. return QColor(255,0,0);
    7. }
    8. //...
    9. }
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Jun 2006
    Posts
    9
    Thanks
    2
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11

    Default Re: Color table row with ItemDelegate

    Thank you for all the hints, but the actual problem is that i don't have a custom data model. I am atm. using a QSqlTableModel ... and i am accessing the model with SQL queries, not directly wit setData().

    Is there a possibility to do the row coloring in the delegate anyway? Or can i set the BackgroundRole using the delegate?

  5. #5
    Join Date
    Jun 2006
    Posts
    9
    Thanks
    2
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11

    Default Re: Color table row with ItemDelegate

    Is the fup question i asked so damn silly, that no one will answer it? Maybe the solution to this problem is so simple that i cannot extract it from your answers?

    The problem is that i cannot set data within the model from the paint() method in the delegate (which is against MVC anyway i guess) but coloring of a row is a view thing, and maybe you want your row colored in a QTableView (like i do) and not in some other representation (perhaps QListView).

    As of my point of view, there has to be a way in which i can manipulate the row color (which is a simple task, i think, and will be used by others too) but i just don't get it.

    Perhaps anyone can tell me how that is possible ...

  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: Color table row with ItemDelegate

    You can do it from the delegate. The core of the functionality will be exactly the same as the one I posted above, you just need to place it in the delegate. Subclass QItemDelegate and reimplement its paint function. You should then insert the already mentioned code there (of course you need to change it slightly but the general idea remains) so that it changes the palette of the "option" param and then call the base class implementation to handle the rest.

    It's possible that this will not work (depends if the default delegate looks at the palette to render the background -- I'm not sure of that). In that case just fill the background yourself using QPainter::fillRect() before calling the base implementation of the paint method.

  7. The following user says thank you to wysota for this useful post:

    dexjam (29th June 2006)

  8. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Color table row with ItemDelegate

    Note that the second solution doesn't use setData() --- only data(), so you can easily subclass QSqlTableModel.

    As for the delegate, maybe something like:
    Qt Code:
    1. void Delegate::paint( QPainter *painter,
    2. const QStyleOptionViewItem& option,
    3. const QModelIndex& index ) const
    4. {
    5. QStyleOptionViewItem modOption( option );
    6. if( rowShouldBeHighlighted( index ) ) {
    7. modOption.palette.setColor( QPalette::Base, Qt::green );
    8. modOption.palette.setColor( QPalette::AlternateBase, Qt::green );
    9. }
    10. QItemDelegate::paint( painter, modOption, index );
    11. }
    To copy to clipboard, switch view to plain text mode 

  9. The following user says thank you to jacek for this useful post:

    dexjam (29th June 2006)

  10. #8
    Join Date
    Jun 2006
    Posts
    9
    Thanks
    2
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11

    Default Re: Color table row with ItemDelegate

    @wysota: You're my man :) using your "watched column" approach i got it working .... if anybody is interested in the code on how to change the row color of a QTableView using a delegate that inherits from QItemDelegate it's printed below ...

    @jacek: your approach seemed not to work ... or maybe i did something wrong ... i did not follow that thing any further ...

    Thanks again for your help ...

    Qt Code:
    1. void Delegate::paint(QPainter *painter,
    2. const QStyleOptionViewItem &option,
    3. const QModelIndex &index) const
    4. {
    5.  
    6. QDateTime compareDate = QDateTime::currentDateTime();
    7. //QDateTime::fromString(dateString, Qt::ISODate);
    8. //compareDate = compareDate.addYears(30);
    9. //qDebug("Date is: %s", qPrintable(compareDate.toString()));
    10.  
    11. QModelIndex testindex = index.model()->index(index.row(), 11, index.parent()); // get index of "watched" column
    12. if( index.model()->data(testindex, Qt::DisplayRole).toDateTime() > compareDate)
    13. {
    14. painter->fillRect(option.rect, Qt::green);
    15. }
    16.  
    17. QItemDelegate::paint( painter, option, index );
    18.  
    19. }
    To copy to clipboard, switch view to plain text mode 

  11. #9
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Color table row with ItemDelegate

    Quote Originally Posted by dexjam
    your approach seemed not to work ...
    I've just checked the sources and QItemDelegate::paint() just ignores option.palette if the item is not selected, so it had no chance to work.

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.