I have an application in which i am entering text from text edit to QListView. when the user enters text smileys like :), it has to replace with smiley images in QListView.
I don't know how to do that. I have also searched on internet but i didn't get any solution for qt.
So please give me any suggestion for this.
ok... can you please tell what is the easiest way to replace smiley text to smiley icon?
i would go like this "example":http://qt-project.org/doc/qt-4.8/richtext-textobject.html.
But instead looking for image you need to parse for your smiley-text and insert an image format(with your smiley image).
When you are finished you can use "QTextDocument::drawContents()":http://qt-project.org/doc/qt-4.8/qtextdocument.html#drawContents in your delegate's paint method.
Note that you will also have to implement the delegate's sizeHint() method and returning the size of the QTextDocument.
But you need to call QTextDocument::setTextWidth() first and then call QTextDocument::size() afterwards.
It should be enough if your delegate holds 1 QTextDocument instance. And everytime you need to get the size or paint it, set the text to the QTextDocument and do the replacement (like in the example) first.
Edit: you may also follow the "smiley" tag for alternative approaches. But i like the text-object approach most, because it preserves the original content instead of changing it. Whatever fits your needs more.
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?
You are missing the space (forum doesn't like it so i inserted an underscore) between "img" and "src".
In my case QString textMessage ="hello <img_src="../emoticons/cute.png">""; is not the problem.
textMessage is not getting displayed in 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.
Image is not issue, i am able to find image.
and the translation of the painter? did it work?
Anyone solved or know how to this exactly the same problem I have?
How should anyone even know what problem you have?!
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.
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.