Implementing chat type listview with text bubbles

  • I am trying to write a little program showing the chat history from a messaging app, trying to stay as close to the original look of the (android) app. The actual chat data is in a SQL database.

    At this moment I am creating a QSqlQueryModel, which holds the data, and a QListView which is supposed to show the data. In between I have a QStyledItemDelegate to do the painting in the view in order to get the custom look of the list items.

    An example of the look I am going for:


    The main things being the bubble frame around the text, and the text having different sizes and possibly containing images (photos, gifs, emoji), video or audio. One of the main things I'm having trouble with, is that when drawing a bubble in the delegate, I can't seem to find out what size the contents are going to be. For normal text I might be able to use QFontMetrics, but with rich text I'm not sure. I tried putting everything in a QTextEdit since it handles rich text, and through the HTML capabilities also images. Then painting that QTextEdit in de delegates paint() method, but the QTextEdit doesn't seem to know its own size either (probably because its image is being painted, but the QTextEdit itself is never actually show()n).

    Am I on the right track with the model/view/delegate? I have also considered QGraphicsView, but I'm not sure if it would help with anything, and I think I really need the lazy loading of the view, since each chat can contain thousands of messages. Any hints on how to implement this? Even just starting getting this to work with messages containing only text would be a great help. Thanks!


  • Lifetime Qt Champion


    AFAIK, you're on the right track. I just stumbled upon this for HTML rendering. Might of interest.

    Hope it helps

  • Thank you very much! After your message I totally ran with it (and that example you provided helped a great deal), and got it mostly working. I have a few small problems still that I need to work out, but I think they would be better off in a new thread as they would be off topic here.

    Thanks again!

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