PDA

View Full Version : Different color letters in QComboBox edit field



T4ng10r
26th February 2014, 08:54
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.

Scorp2us
26th February 2014, 14:23
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"

T4ng10r
27th February 2014, 21:07
Unfortunately this approach isn't working.

T4ng10r
4th March 2014, 08:10
I tried something different. As you may notice - QComboBox::paintEvent() is

QStylePainter painter(this);
painter.setPen(palette().color(QPalette::Text));

// draw the combobox frame, focusrect and selected etc.
QStyleOptionComboBox opt;
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.

void SubstituteSearchResultComboBox::paintEvent(QPaintE vent * event)
{
int start_pos(0), prev_pos(0);
QStylePainter painter(this);
painter.setPen(palette().color(QPalette::Text));

QStyleOptionComboBox opt;
initStyleOption(&opt);
painter.drawComplexControl(QStyle::CC_ComboBox, opt);

QStyleOptionComboBox cmb;
initStyleOption(&cmb);

QString text_;
QFontMetrics font_metric(font());

QModelIndex index = model()->index(currentIndex(),0);
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::Wi ndowText));
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);
}