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::paintEvent() 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.
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"
Re: Different color letters in QComboBox edit field
Unfortunately this approach isn't working.
[SOLVED] Different color letters in QComboBox edit field
I tried something different. As you may notice - QComboBox::paintEvent() is
Code:
painter.
setPen(palette
().
color(QPalette::Text));
// draw the combobox frame, focusrect and selected etc.
initStyleOption(&opt);
painter.
drawComplexControl(QStyle::CC_ComboBox, opt
);
// draw the icon and text
painter.
drawControl(QStyle::CE_ComboBoxLabel, opt
);
For my needs - I only need to rewrite last line reponsible for drawing label/edit field (icon and text). And it worked.
Code:
void SubstituteSearchResultComboBox
::paintEvent(QPaintEvent * event
) {
int start_pos(0), prev_pos(0);
painter.
setPen(palette
().
color(QPalette::Text));
initStyleOption(&opt);
painter.
drawComplexControl(QStyle::CC_ComboBox, opt
);
initStyleOption(&cmb);
QString text
= model
()->data
(index
).
toString();
SuccessWord success_word = model()->data(index,Qt::UserRole).value<SuccessWord>();
QPen standard_pen
= painter.
pen();
standard_pen.
setColor(palette
().
color(QPalette::WindowText));
QPen special_pen
= painter.
pen();
special_pen.
setColor(QColor(Qt
::red));
special_pen.setWidth(special_pen.width()+1);
QRect rect_
= painter.
style()->proxy
()->subControlRect
(QStyle::CC_ComboBox,
&cmb,
QStyle::SC_ComboBoxEditField,
this);
current_width = rect_.left();
for(char letter : success_word.matchingLetters)
{
start_pos = text.indexOf(letter, prev_pos, Qt::CaseInsensitive);
if (start_pos<0) break;
text_ = text.mid(prev_pos, start_pos-prev_pos);
drawItem(&painter, rect_ , text_, font_metric.width(text_), standard_pen, current_width);
prev_pos = start_pos;
text_ = text.mid(prev_pos, 1);
drawItem(&painter, rect_ , text_, font_metric.width(text_), special_pen, current_width);
prev_pos++;
}
text_ = text.mid(prev_pos);
drawItem(&painter, rect_ , text_, font_metric.width(text_)+5, standard_pen, current_width);
}