Results 1 to 3 of 3

Thread: tableView for files: give one type a different colour: why doesn't this work?

  1. #1
    Join Date
    Jul 2009
    Posts
    92
    Thanks
    7
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default tableView for files: give one type a different colour: why doesn't this work?

    using the stardelegate example I made a simple paint function to make a file with extension "001" blue:
    Qt Code:
    1. void StarDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option,
    2. const QModelIndex &index) const
    3. {
    4. QStyleOptionViewItem opt = option;
    5. if (index.column() == 0)
    6. {
    7. QString name = index.model()->data(index, Qt::DisplayRole).toString();
    8. if (QFileInfo(name).suffix() == "001")
    9. {
    10. painter->setPen(QColor(0, 0, 255, 255));
    11. QFont f = painter->font();
    12. painter->setFont(f);
    13. opt.font.setItalic(true);
    14. }
    15. }
    16. QStyledItemDelegate::paint(painter, opt, index);
    17. }
    To copy to clipboard, switch view to plain text mode 
    The files with 001 are displayed in italics but not in blue. So the selection and adapting opt works, but setPen doesn't. Why not? thanks.

  2. #2
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: tableView for files: give one type a different colour: why doesn't this work?

    No, this doesn't work because the settings made to the painter are only local.
    When the painting is done (as in being performed), the actual style is used and your changes to the painter are lost.

    Unless, of course, you do the painting of the text directly in the delegate.

    Like the italics, set the color in the option. Then it will work as the style makes use of the option (or should make use of it)

  3. #3
    Join Date
    Jul 2009
    Posts
    92
    Thanks
    7
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: tableView for files: give one type a different colour: why doesn't this work?

    thanks, using this now

    if (QFileInfo(name).suffix() == "001")
    opt.palette.setColor(QPalette::Text,QColor(0, 0, 255, 255) );

    and it works

Similar Threads

  1. Replies: 0
    Last Post: 26th February 2010, 16:30
  2. Replies: 0
    Last Post: 19th February 2010, 17:33
  3. Pack all .qm files into .qrc, why doesn't work???
    By yxmaomao in forum Qt Programming
    Replies: 3
    Last Post: 8th May 2008, 20:38
  4. QRegExp doesn't seem to work as it should
    By thomaspu in forum Qt Programming
    Replies: 3
    Last Post: 21st December 2007, 08:49
  5. setMouseTracking() doesn't work.
    By luffy27 in forum Qt Programming
    Replies: 13
    Last Post: 27th April 2007, 19:16

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.