Answer the question
In order to leave comments, you need to log in
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
No, the user does not enter text. Already thinking about QLabel. Probably with his help I will solve the problem
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 questionAsk a Question
731 491 924 answers to any question