Qtwidget: use the new features of qt4.5 to implement a cool and transparent form

Source: Internet
Author: User
Tags gz file

Qt4.2 introduces the qwidget: setwindowopacity function. You can set the transparency for the form. The smaller the value, the more transparent the value is. The set form can display transparent effects as a whole. However, this setting is rough and can only have one overall effect. It can be used only for example when you drag the image. Most of the time it is not practical. The new form transparency feature is introduced in qt4.5, which is the attribute of a widget, called QT: wa_translucentbackground. This attribute can be set separately for each qwidget, and the transparency level can be controlled by the drawn color or the alpha channel value of the image.

I wrote an example to demonstrate its amazing effect. First look at one:

This is the result of my example. The background is www.cuteqt.com. The following describes how to implement the Code.

Translucentbackground controls transparent form attributes
In this example, a qwidget is used on the main interface, and four controls are placed on it. The two sub-classes are custom qwidgets. A transparent background image is drawn in paintevent, and the words "cuteqt" are written in the book; the following two are standard qlabel controls, but two different effects are displayed.

The translucentbackground attribute of transparent controls is true (inherits the parent attribute), while non-transparent controls forcibly set translucentbackground to false in code, which creates interesting results. The code snippet is as follows:
Label = new qlabel ("www.cuteqt.com ");
Label-> setattribute (QT: wa_translucentbackground, false );
Label-> setautofillbackground (true );

Alpha Channel controls transparency
In this example, modify the Alpha value of the background color to show different transparency. The method is to set the palette attribute of the form and set the Alpha value for the colorrole color of background. The code snippet is as follows:
Qpalette pal = palette ();
Pal. setcolor (qpalette: Background, qcolor (255, 0, 0,200 ));
Setpalette (PAL );

The different display effects of alpha values 100 and 200 are shown.

How is this example interesting? Download the complete code to learn more ~ Do you have any questions about blog or BBS messages ~

Translucent.tar.gz

All posts on this site are reserved by this site and the original author. This article can only be reproduced if the copyright information, original article links, and original article authors are retained. Do not delete or modify the original article content for reprinting, it is not intended for commercial purposes. Thank you for your cooperation. Http://www.cuteqt.com/blog? P = 340

Posted on Ben's snail reading (1781) Comments (6) EDIT favorite Reference Category: QT custom Widgets


Comment

Use the new features of qt4.5 to implement the cool transparent form [not logged on] "href =" http://www.cppblog.com/biao/archive/2009/06/12/87508.html#88511 ">#re: qtwidget: use the new features of qt4.5 to implement a cool and transparent form [not logged on] Haha Oh? Prepare to try ~ Reply to more comments

# Re: qtwidget: use the new features of qt4.5 to implement a cool and transparent form. Hello, dotboy, I tried your code on the Windows platform, the result is that the background of the main form turns black and the desktop is not displayed... reply to more comments

# Re: qtwidget: use the new features of qt4.5 to implement a cool transparent form at on. In Windows, you have to pass the title bar to display the transparent effect ...{
Return
Setreplyauhor ("dotboy ")
} 'Href = "http://www.cppblog.com/biao/archive/2009/06/12/87508.html#post"> reply to more comments

# Re: qtwidget: use the new features of qt4.5 to implement a cool transparent form holy GZ file format? It cannot be decompressed with WinRAR!
Please use WinRAR to pack it and reply to more comments.

# Re: qtwidget: use the new features of qt4.5 to implement a cool and transparent form [not logged on] May I ask if the windows system can achieve transparent results? reply to more comments

# Re: qtwidget: use the new features of qt4.5 to implement a cool transparent form [not logged on] 2010-02-23. The key phrase "The translucentbackground attribute of the transparent control is true (inherits the parent attribute ), A non-transparent control forcibly sets translucentbackground to false in the code ,"{
Return
Setreplyauhor ("Lu Ren jia ")
} 'Href = "http://www.cppblog.com/biao/archive/2009/06/12/87508.html#post"> reply to more comments from: http://www.cppblog.com/biao/archive/2009/06/12/87508.html

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.