Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Replace text smiley with images in QListView
Forum Updated to NodeBB v4.3 + New Features

Replace text smiley with images in QListView

Scheduled Pinned Locked Moved General and Desktop
16 Posts 4 Posters 5.7k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • raven-worxR Offline
    raven-worxR Offline
    raven-worx
    Moderators
    wrote on last edited by
    #4

    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.

    --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
    If you have a question please use the forum so others can benefit from the solution in the future

    1 Reply Last reply
    0
    • P Offline
      P Offline
      psbhardwaj09gmail.com
      wrote on last edited by
      #5

      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.

      Pardeep Sharma

      1 Reply Last reply
      0
      • raven-worxR Offline
        raven-worxR Offline
        raven-worx
        Moderators
        wrote on last edited by
        #6

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

        --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
        If you have a question please use the forum so others can benefit from the solution in the future

        1 Reply Last reply
        0
        • P Offline
          P Offline
          psbhardwaj09gmail.com
          wrote on last edited by
          #7

          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

          Pardeep Sharma

          1 Reply Last reply
          0
          • raven-worxR Offline
            raven-worxR Offline
            raven-worx
            Moderators
            wrote on last edited by
            #8

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

            }
            @

            --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
            If you have a question please use the forum so others can benefit from the solution in the future

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #9

              To add to raven_worx, are you sure that your images can be found ? Using relative path can be tricky

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              0
              • P Offline
                P Offline
                psbhardwaj09gmail.com
                wrote on last edited by
                #10

                Hi SGalst,

                Image is not issue, i am able to find image.

                Pardeep Sharma

                1 Reply Last reply
                0
                • raven-worxR Offline
                  raven-worxR Offline
                  raven-worx
                  Moderators
                  wrote on last edited by
                  #11

                  and the translation of the painter? did it work?

                  --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
                  If you have a question please use the forum so others can benefit from the solution in the future

                  1 Reply Last reply
                  0
                  • Z Offline
                    Z Offline
                    zgulser
                    wrote on last edited by
                    #12

                    Hi fellas,

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

                    1 Reply Last reply
                    0
                    • raven-worxR Offline
                      raven-worxR Offline
                      raven-worx
                      Moderators
                      wrote on last edited by
                      #13

                      [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?!

                      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
                      If you have a question please use the forum so others can benefit from the solution in the future

                      1 Reply Last reply
                      0
                      • Z Offline
                        Z Offline
                        zgulser
                        wrote on last edited by
                        #14

                        I meant the problem presented in this thread

                        1 Reply Last reply
                        0
                        • raven-worxR Offline
                          raven-worxR Offline
                          raven-worx
                          Moderators
                          wrote on last edited by
                          #15

                          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.

                          --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
                          If you have a question please use the forum so others can benefit from the solution in the future

                          1 Reply Last reply
                          0
                          • Z Offline
                            Z Offline
                            zgulser
                            wrote on last edited by
                            #16

                            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.

                            1 Reply Last reply
                            0

                            • Login

                            • Login or register to search.
                            • First post
                              Last post
                            0
                            • Categories
                            • Recent
                            • Tags
                            • Popular
                            • Users
                            • Groups
                            • Search
                            • Get Qt Extensions
                            • Unsolved