Results 1 to 3 of 3

Thread: [SOLVED] Different color letters in QComboBox entries

  1. #1
    Join Date
    Feb 2007
    Location
    Wroclaw, Poland
    Posts
    72
    Thanks
    6
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default [SOLVED] Different color letters in QComboBox entries

    I would like to draw QComboBox items letters in different color. Lets assume that I have "Test12" entry and "st" in it I want to be bold red and rest to be plain black.
    So to make think easier - "Te<b>st</b>12" (or whichever other tag to mark letters). I created QItemDelegate with paint method.
    Qt Code:
    1. inline void paint(QPainter *painter, const QStyleOptionViewItem &option,
    2. const QModelIndex &index ) const
    3. {
    4. int correction(0);
    5. QStyleOptionViewItemV4 myOption = option;
    6. QPalette standard_color = myOption.palette;
    7. QPalette special_color = myOption.palette;
    8. myOption.textElideMode = Qt::ElideNone;
    9. QFontMetrics font_metric(myOption.font);
    10. special_color.setColor(QPalette::WindowText, QColor(Qt::red));
    11.  
    12. QString text = "Teno<b>st</b>1989";
    13. int start_pos(0), prev_pos(0), current_width(0);
    14. std::string word;
    15. while((start_pos=text.indexOf(start_tag, prev_pos))>=0)
    16. {
    17. myOption.text = text.mid(prev_pos, start_pos-prev_pos);
    18. //draw in standard color
    19. myOption.palette = standard_color;
    20. myOption.rect.setLeft(current_width);
    21. myOption.rect.setWidth(font_metric.width(myOption.text)+correction);
    22. QApplication::style()->drawControl(QStyle::CE_ItemViewItem, &myOption, painter);
    23. current_width += myOption.rect.width();
    24. prev_pos = start_pos+start_tag.size();
    25.  
    26. start_pos=text.indexOf(end_tag, prev_pos);
    27. myOption.text = text.mid(prev_pos, start_pos-prev_pos);
    28. myOption.palette = special_color;
    29. myOption.rect.setLeft(current_width);
    30. myOption.rect.setWidth(font_metric.width(myOption.text)+correction);
    31. QApplication::style()->drawControl(QStyle::CE_ItemViewItem, &myOption, painter);
    32. current_width += myOption.rect.width();
    33. prev_pos = start_pos+end_tag.size();
    34. }
    35. myOption.text = text.mid(prev_pos);
    36. myOption.palette = standard_color;
    37. myOption.rect.setLeft(current_width);
    38. myOption.rect.setWidth(font_metric.width(myOption.text));
    39. QApplication::style()->drawControl(QStyle::CE_ItemViewItem, &myOption, painter);
    40. }
    To copy to clipboard, switch view to plain text mode 

    Unfortunately - CE_ItemViewItem do not only write text but also frame. I tried QStyle::drawItemText, but I'm still not satisfied. How else could I do it?
    Last edited by T4ng10r; 19th February 2014 at 10:55.

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,419
    Thanks
    37
    Thanked 1,545 Times in 1,495 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Different color letters in QComboBox entries

    What about letting CE_ItemViewItem not see any text, i.e. keeping myOption.text empty, and the drawing the text yourself?

    Cheers,
    _

  3. The following user says thank you to anda_skoa for this useful post:

    T4ng10r (19th February 2014)

  4. #3
    Join Date
    Feb 2007
    Location
    Wroclaw, Poland
    Posts
    72
    Thanks
    6
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Different color letters in QComboBox entries

    Yes, this approach do what I expect.

Similar Threads

  1. how to change the QComboBox focus color?
    By josentop in forum Qt Programming
    Replies: 1
    Last Post: 18th December 2011, 16:20
  2. Add separator to QComboBox using QStringList entries
    By mrknight in forum Qt Programming
    Replies: 3
    Last Post: 26th November 2010, 23:31
  3. Setting specific entries of a QComboBox to appear Bold
    By prykHetQuo in forum Qt Programming
    Replies: 7
    Last Post: 23rd March 2009, 23:11
  4. Editing and adding entries to a QComboBox
    By bruccutler in forum Newbie
    Replies: 1
    Last Post: 29th May 2007, 17:13
  5. Qt4.1.1: Sorting QCombobox entries
    By Byngl in forum Qt Programming
    Replies: 4
    Last Post: 17th March 2006, 09:02

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.