How to make the qtextedit background transparent)

Source: Internet
Author: User

ForwardArticleBytes. Let's talk about how to make the background of the qtextedit control transparent.

 

As mentioned in the previous article, all problems in QT should be discussed in two systems: qwidget system and qgraphicswidget system. This is no exception.

 

1. For the qwidget system: qwidget and its derived class are used. Here: the parent object of qtextedit is also a qwidget or its derived class.

So what we need to do is to make the qtextedit background transparent, and then fl at the position of the parent window behind it.

 

① Set qtextedit as transparent Background:

Qpalette PL = iedit-> palette ();

Pl. setbrush (Qpalette: Base, Qbrush (qcolor (, 0 )));

Iedit-> setpalette (PL );

 

That is, you can click the background of the edit box with a completely transparent paint brush!

 

When the parent window is in this position, the key is to pay attention to whether the parent window is a top-level window (with no parent window). If so, be sure not to use setstylesheet () to brush (reason see article http://blog.csdn.net/NRC_DouNingBo/archive/2010/05/07/5565212.aspx ).

 

 

II. For the qgraphicswidget system, use the following method.CodeNow:

 

Mainwindow: mainwindow (qwidget * parent)
: Qgraphicsview (parent)
{
This-> resize( 360,640 );
Iscene = new qgraphicsscene (0, 0, 360,640 );

Iedit = new qtextedit ();
Iedit-> resize( 360,400 );

 

// Click the image below to give the parent view a picture
Qpalette palette;
Palette. setbrush (this-> backgroundrole (), qbrush (qimage (":/BMP/dou.jpg ")));
This-> setpalette (palette );

 

// This section is used to set the background transparency of the edit box item.

Palette. setbrush (qpalette: Base, qbrush (qcolor (255, 0, 0 )));
Iedit-> setpalette (palette );

Qgraphicsproxywidget * widget = iscene-> addwidget (iedit );

Palette. setbrush (Qpalette: window,Qbrush (qcolor )));
Widget-> setpalette (palette );

This-> setscene (iscene );
}

 

It can be seen that there is still a lot of difference, and it seems difficult to understand some places. In fact, the key involves two problems. What is the difference between the qwidget system and the qgraphicswidget system? One is the difference between two ways to set the background color/image using the style sheet qpalette (that is, the first parameter of the setbrush () function of qpalette is qpalette :: the base format is the difference between PTR-> backgrounrole ).

 

I will write two or more articles to discuss these two issues, but you can directly copy and use the above Code.

 

Test environment: qt4.5.0 + vc2005

 

Post-completion:

1: differences between qwidget system and qgraphicswidget System

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

 

2: How to Use the style sheet qpalette in QT and related precautions

Http://blog.csdn.net/NRC_DouNingBo/archive/2010/05/09/5571187.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.