Add a background image to qtextedit and precautions

Source: Internet
Author: User

I encountered a very interesting problem some time ago. I want to add a background image to the qtextedit QT space. I have tried many methods and used the qpalette palette and paintevent (), but the results have no effect, later, we found that there are two ways to achieve this: (if you have a better solution, please let me know)

 

Qtextedit * iedit = new qtextedit ();

 

1: Use a style sheet:

Iedit-> setstylesheet ("background-image: URL (:/BMP/dscn1604.jpg )");

 

Note: The ":" colon that begins first in the URL () brackets must not be wrong, otherwise it cannot be displayed.

 

2: Use HTML

Iedit-> sethtml ("<body background =/"./BMP/dscn1604.jpg/"> </body> ");

 

Note: In this case, you do not need to use the ":" colon as above. You can simply use the relative path.

 

(Notice: before using the above two methods, do not forget to add the image to the qrc resource file)

// Certificate //-------------------------------------------------------------------------------------------------------------------------------------------------

 

In fact, the above two methods show the same effect, so each method is similar, but they both have two problems:

 

Problem 1: The image cannot be scaled to match the size of the editing box. After all, the path strength of the image is used, so we cannot scale it. Therefore, it is best to set the size of the editing box to the same as the image size before use!

 

Problem 2: This is serious.

When we write more than one screen: the edit box needs to be displayed, and you will find that the image will follow the page.

 

As shown in the following figure:

Of course, I want this image to be fixed behind the editing box instead of being flipped. But if I use the above method to set a background image, when I write more words than one screen: the image will also follow the screen flip, which may occur as follows:

 

That is, the image will be tiled multiple times behind the editing box, which is obviously not what we want.

 

I tried to solve this problem, but I didn't find a good solution. I finally had to use the "Submit for perfection" method, that is, I set the edit box to transparent, you can see the widget window behind the widget through it, and then I can brush this picture at this position of the widget! This method works well !!

 

For more information about how to make the qtextedit background transparent, see the article:

Http://blog.csdn.net/NRC_DouNingBo/archive/2010/05/09/5571088.aspx

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.