Replace text smiley with images in QListView
-
Hi
I have used QTextDocument::drawContents() to display the smiley images in custom Delegate QListView. I am facing problem to display the textMessage (with smiley images) in QListView. Below is the code to display the textMessage in QListView
@
void ConversationViewItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option) const
{
QRect messageRect = option.rect;QString textMessage ="hello <imgsrc="../emoticons/cute.png">""; //text message with image tag
QTextDocument *doc = new QTextDocument();
doc->setHtml(textMessage);
doc->drawContents(painter, messageRect);//Perhaps this is not workingpainter->restore();
}
@textMessage contains the image path in tag which is to be displayed <imgsrc="../emoticons/angry.png">"). doc->setHtml(textMessage) sets the doc with textMessage. I need to display this doc containing textMessage in messageRect. But doc->drawContents(painter, messageRect) is not displaying textMessage in messageRect of QListView.
-
is this really your code?
@
QString textMessage ="hello <imgsrc="../emoticons/cute.png">"";
@
if so change it to:
@
QString textMessage ="hello <img_src="../emoticons/cute.png">"";
@
You are missing the space (forum doesn't like it so i inserted an underscore) between "img" and "src". -
Hi
In my case QString textMessage ="hello <img_src="../emoticons/cute.png">""; is not the problem.
textMessage is not getting displayed in messageRect.
but when i am using
@doc->drawContents(painter); @instead of
@doc->drawContents(painter, messageRect);@
textMessage is displaying in QListView. But there is also a problem that when i again add another message to list then it gets overlapped with old one.Rgds,
Pardeep -
ok i see ... try this:
@
void ConversationViewItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option) const
{
painter->save();
....
painter->translate( option.rect.topLeft() );QTextDocument *doc = new QTextDocument(); doc->setHtml(textMessage); doc->drawContents(painter, messageRect); painter->restore();
}
@ -
To add to raven_worx, are you sure that your images can be found ? Using relative path can be tricky
-
Hi SGalst,
Image is not issue, i am able to find image.
-
and the translation of the painter? did it work?
-
Hi fellas,
Anyone solved or know how to this exactly the same problem I have?
-
[quote author="zgulser" date="1387383975"]
Anyone solved or know how to this exactly the same problem I have?[/quote]How should anyone even know what problem you have?!
-
I meant the problem presented in this thread
-
the thread author stopped responding in this thread...
Maybe you can provide a minimal compileable example. I am pretty sure it's a small thing missing to make the example form above to work. -
Ok raven. Here is the part of the code that is working but not as expected;
@
....QRect messageContentRect = messageFontMetrics.boundingRect(textOffset4,
rect.top() + textHeightInPixels + 13,
rect.width() - (textOffset4) - dateTimeTextWidthInPixels - 5 - 10 , 0,
Qt::AlignLeft | Qt::AlignTop | Qt::TextWrapAnywhere,
messageContent);painter->setPen(QColor("#363636"));
messageContent = smilifyMessage(messageContent);
QTextDocument *doc = new QTextDocument();
doc->setHtml(messageContent);
//painter->drawText(messageContentRect, Qt::AlignLeft | Qt::AlignTop |Qt::TextWrapAnywhere, //messageContent, &actuallyUsedRectForMessage);
doc->drawContents(painter, messageContentRect);....
@
QPainter::drawText () is working as expected and messages shown in the view. But if I use QTextDocument::drawContents() method instead, then messages are not shown.