Results 1 to 4 of 4

Thread: Different color letters in QComboBox edit field

  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 Different color letters in QComboBox edit field

    User selects one of possible entries from drop-down list (already drawn with some letters in other then default color). But this selected entry is displayed without color letters (which is understandable). How can I draw/print this text with my own drawing procedure to color some of letters?
    I tried to work with paintEvent(). First I call QComboBox:aintEvent() so it would draw default control. Then I tried to use painter->drawTextItem(). Unfortunately - rect for this operation is different then whole control and my new colorful text didn't covered previous one. I don't want to use hardcoded tweaks and values to match my text and the one draw by default.
    Are there other possibilities? Once again - I want to draw selected QComboBox text with my own color rules.

  2. #2
    Join Date
    Apr 2008
    Posts
    45
    Thanks
    3
    Thanked 2 Times in 2 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Different color letters in QComboBox edit field

    Qt supports in some places a simplified HTML syntax. It's been a while but I am pretty sure that you can colorize items in the drop down. I didn't try it this way, but it should work:
    "<font color="blue">Blue</font> to <font color="red">Red</font>" which shoudl give you an item "Blue to Red"

  3. #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 edit field

    Unfortunately this approach isn't working.

  4. #4
    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 edit field

    I tried something different. As you may notice - QComboBox::paintEvent() is
    Qt Code:
    1. QStylePainter painter(this);
    2. painter.setPen(palette().color(QPalette::Text));
    3.  
    4. // draw the combobox frame, focusrect and selected etc.
    5. initStyleOption(&opt);
    6. painter.drawComplexControl(QStyle::CC_ComboBox, opt);
    7.  
    8. // draw the icon and text
    9. painter.drawControl(QStyle::CE_ComboBoxLabel, opt);
    To copy to clipboard, switch view to plain text mode 
    For my needs - I only need to rewrite last line reponsible for drawing label/edit field (icon and text). And it worked.
    Qt Code:
    1. void SubstituteSearchResultComboBox::paintEvent(QPaintEvent * event)
    2. {
    3. int start_pos(0), prev_pos(0);
    4. QStylePainter painter(this);
    5. painter.setPen(palette().color(QPalette::Text));
    6.  
    7. initStyleOption(&opt);
    8. painter.drawComplexControl(QStyle::CC_ComboBox, opt);
    9.  
    10. initStyleOption(&cmb);
    11.  
    12. QString text_;
    13. QFontMetrics font_metric(font());
    14.  
    15. QModelIndex index = model()->index(currentIndex(),0);
    16. QString text = model()->data(index).toString();
    17. SuccessWord success_word = model()->data(index,Qt::UserRole).value<SuccessWord>();
    18.  
    19. QPen standard_pen = painter.pen();
    20. standard_pen.setColor(palette().color(QPalette::WindowText));
    21. QPen special_pen = painter.pen();
    22. special_pen.setColor(QColor(Qt::red));
    23. special_pen.setWidth(special_pen.width()+1);
    24. QRect rect_ = painter.style()->proxy()->subControlRect(QStyle::CC_ComboBox, &cmb, QStyle::SC_ComboBoxEditField, this);
    25.  
    26. current_width = rect_.left();
    27. for(char letter : success_word.matchingLetters)
    28. {
    29. start_pos = text.indexOf(letter, prev_pos, Qt::CaseInsensitive);
    30. if (start_pos<0) break;
    31.  
    32. text_ = text.mid(prev_pos, start_pos-prev_pos);
    33. drawItem(&painter, rect_ , text_, font_metric.width(text_), standard_pen, current_width);
    34. prev_pos = start_pos;
    35.  
    36. text_ = text.mid(prev_pos, 1);
    37. drawItem(&painter, rect_ , text_, font_metric.width(text_), special_pen, current_width);
    38. prev_pos++;
    39. }
    40. text_ = text.mid(prev_pos);
    41. drawItem(&painter, rect_ , text_, font_metric.width(text_)+5, standard_pen, current_width);
    42. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by T4ng10r; 4th March 2014 at 09:14.

Similar Threads

  1. [SOLVED] Different color letters in QComboBox entries
    By T4ng10r in forum Qt Programming
    Replies: 2
    Last Post: 19th February 2014, 11:54
  2. How to force capital letters in input field?
    By falconium in forum Newbie
    Replies: 12
    Last Post: 8th May 2012, 13:07
  3. QComboBox in QTreeView on Edit?
    By AyaKoshigaya in forum Qt Programming
    Replies: 1
    Last Post: 27th March 2012, 19:16
  4. Replies: 2
    Last Post: 23rd December 2010, 09:53
  5. Replies: 1
    Last Post: 15th January 2009, 11:34

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.