Replace text smiley with images in QListView



  • Hi,

    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.

    Rgds,
    Pardeep


  • Lifetime Qt Champion

    Hi,

    The thing that comes to my mind would be a QStyleItemDelegate where you parse your string for smileys when doing the painting



  • ok... can you please tell what is the easiest way to replace smiley text to smiley icon?


  • Moderators

    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.



  • 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 working

    painter->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.


  • Moderators

    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


  • Moderators

    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();
    

    }
    @


  • Lifetime Qt Champion

    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.


  • Moderators

    and the translation of the painter? did it work?



  • Hi fellas,

    Anyone solved or know how to this exactly the same problem I have?


  • Moderators

    [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


  • Moderators

    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() - (textOffset
    4) - 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.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.