The text drawn by paint scrolls with the scroll bar
-
Hello
I am using the QItemDelegate to draw text elements in the QListWidget.
There are some text elements that must have highlighted word inside. I implemented this by separate drawing of one string.
There is a problem with horizontal scrolling of this text. When I am trying to scroll the text, it moves with the scrollbar. .
This is the paint function.
void paint(QPainter painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
painter->save();
QString string = index.data().toString();
QString keyword = qobject_cast<QLineEdit>(this->parent())->text();
if(keyword != "" && string.contains(keyword))
{
QRect rect = option.rect;
rect.setWidth(QFontMetrics(painter->font()).size(Qt::TextSingleLine, string).width());
int index;
QString painted = "";
while(string.contains(keyword))
{
index = string.indexOf(keyword);
QString buffer = string.mid(0,index);painter->setPen(Qt::black); painter->drawText(rect, buffer); rect.setX(QFontMetrics(painter->font()).size(Qt::TextSingleLine, buffer + painted).width()); painter->setPen(Qt::red); painter->drawText(rect,keyword); buffer = buffer + keyword; string = string.mid(buffer.size()); rect.setX(QFontMetrics(painter->font()).size(Qt::TextSingleLine, buffer + painted).width()); painted = painted + buffer; } painter->setPen(Qt::black); painter->drawText(rect, string); } else { painter->setPen(Qt::black); painter->drawText(option.rect, string); } painter->restore(); }
-
@KAMblLLl said in The text drawn by paint scrolls with the scroll bar:
There are some text elements that must have highlighted word inside
You could also use https://doc.qt.io/qt-6/qsyntaxhighlighter.html