P
P
Pavel2014-03-13 20:25:08
Qt
Pavel, 2014-03-13 20:25:08

Qt: how to color highlight words in text in QLineEdit widget?

Good afternoon.
There was a need to highlight a word in the text using Qt. In particular, in the QLineEdit widget. On various forums, it is advised to highlight text with html tags, for example: text. But the disadvantage of this approach is that, for example, QLineEdit does not support html markup.
How can I use Qt to color just a word in a text, and not the whole text?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
P
Pavel, 2014-03-13
@FluffyMan

The parser ate the html markup in the example I gave.

P
Pavel, 2014-03-14
@FluffyMan

No, the user does not enter text. Already thinking about QLabel. Probably with his help I will solve the problem

V
Vladimir, 2014-04-08
@radianto

Solved a similar issue.
void setLineEditTextFormat(QLineEdit* lineEdit, const QList& formats)
{
if(!lineEdit)
return;
QList attributes;
foreach(const QTextLayout::FormatRange& fr, formats)
{
QInputMethodEvent::AttributeType type = QInputMethodEvent::TextFormat;
int start = fr.start - lineEdit->cursorPosition();
int length = fr length;
QVariant value = fr.format;
attributes.append(QInputMethodEvent::Attribute(type, start, length, value));
}
QInputMethodEvent event(QString(), attributes);
QCoreApplication::sendEvent(lineEdit, &event);
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question