Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to give QTextFrame or QTextBlock a background-image in QTextEdit?

Tags:

qt

qtextedit

I am developing an IM tool,as a part of it I have to develop a BubbleChatWidget on which all message items have a bubble-like background-image.I thought I could achieve my goal with QTextEidt,but I don't know how to give 'QTextFrame' or QTextBlock a background-image.

So my question is that how to give QTextFrame or QTextBlock a background-image in QTextEdit?If QTextEdit can't satisfy my demands, how to achieve my goal with other Qt techniques?

The BubbleChatWidget may contains clickable texts or pictures.And you can't forget the fact that the BubbleChatWidget may contains thousands of items.

The picture below displays what I want.

picture description

like image 863
waterd Avatar asked Sep 10 '14 03:09

waterd


1 Answers

Qt Style Sheets are a perfect fit to achieve what you want.

The solution is to use a border image. Fortunately you can do that with style sheets and you can style QTextEdits.

About styling the QTextBlocks or QTextFrames: A QTextEdit is a widget that displays a QTextDocument which can contain QTextBlocks and QTextFrames. Frames and blocks are text containers that provide structure for the text in a document but they're not rendered by separate widgets. For that reason they can not be styled independently. What I recommend is to use a QTextEdit or other widget for each message and properly manage the consequent increase in memory use.

I'll expose how to style a text edit.

First, take a clean image of your desired border. With a little Photoshop I've prepared my own image (not as clean as it should for a production app):

bkg.png

Lets style objects of the class QTextEdit and its subclasses.

QTextEdit {
    background-color: #eaedf2;              /* Same gray in your background center */
    border-image: url(":/images/bkg.png");  /* The border image                    */
    border-top-width: 11px;
    border-right-width: 4px;
    border-bottom-width: 4px;
    border-left-width: 11px;
}

Setting the previous style sheet to the container of the text edits will turn all of them into this

enter image description here

Something quite similar to your desired look. Of course you have to prepare a good border image and better adjust the border dimensions but I think this could be of help.

If you want different styles for incoming and outgoing messages then you will have to properly differentiate and select them in the style sheet. Check this for reference.

like image 103
mhcuervo Avatar answered Sep 17 '22 00:09

mhcuervo