Qt Shadow border (GO)

Source: Internet
Author: User

Original address: http://blog.sina.com.cn/s/blog_a6fb6cc90101eoc7.html

Shadow border is very common, such as 360 and many other software have similar effect, understand CSS3 students should all know Box-shadow, it is to set the shadow effect, then QT? Read some information, said is QSs is based on CSS2, so, Box-shadow is based on CSS3! Then QT will not be used!

Search some information, each picture is made of shadow effect is impossible, directly discard (even if can, also do not adopt). If the real-time to paint, the efficiency is too low, the last choice of the way the puzzle! The effect is as follows:         top left corner, lower left corner, upper right corner, lower right corner, top, bottom, left, right, these directions are plotted in the corresponding map!   #include "shadow_widget.h"  shadowwidget::shadowwidget (qwidget *parent)     : Qdialog (parent) {    setwindowflags (Qt::framelesswindowhint | Qt::D ialog);     setattribute (Qt::wa_translucentbackground);}  shadowwidget::~shadowwidget () { } void shadowwidget::p aintevent (qpaintevent *event) {     qpainter painter (this);     this->drawshadow (painter);     painter.setpen (Qt::nopen);     painter.setbrush (Qt::white);     Painter.drawrect (Qrect (Shadow_width, Shadow_width, This->width () -2*shadow_width, This->height () -2*SHADOW_ WIDTH));}  void shadowwidget::d rawshadow (Qpainter &painter) {    //draw upper left, lower left, upper right, lower right, top, bottom, left, right border     qlist Pixmaps;    pixmaps.append (QPixmap (":/shadow/shadow_left")); NBsp;   pixmaps.append (Qpixmap (":/shadow/shadow_right"));     pixmaps.append ( Qpixmap (":/shadow/shadow_top"));     pixmaps.append (Qpixmap (":/shadow/shadow_bottom"));     pixmaps.append (Qpixmap (":/shadow/shadow_left_top"));     Pixmaps.append (Qpixmap (":/shadow/shadow_right_top"))     pixmaps.append (QPixmap (":/shadow/ Shadow_left_bottom "));     pixmaps.append (Qpixmap (":/shadow/shadow_right_bottom "));      painter.drawpixmap (0, 0, shadow_width, Shadow_width, pixmaps[4]);     Painter.drawpixmap (This->width ()-shadow_width, 0, Shadow_width, Shadow_width, pixmaps[5]);     painter.drawpixmap (0,this->height ()-shadow_width, Shadow_width, Shadow_width, pixmaps[6]);     painter.drawpixmap (This->width ()-shadow_width, This->height ()-shadow_width, SHADOW_WIDTH, Shadow_width, Pixmaps[7]);     painter.drawpixmap (0, Shadow_width, Shadow_width, This->height () -2*SHADOW_WIDTH, Pixmaps[0].scaled (Shadow_width, This->height () -2*shadow_width));     painter.drawpixmap ( This->width ()-shadow_width, Shadow_width, Shadow_width, This->height () -2*shadow_width, pixmaps[1].scaled ( Shadow_width, This->height ()-2*shadow_width);     painter.drawpixmap (SHADOW_WIDTH, 0, This->width () -2*shadow_width, Shadow_width, pixmaps[2].scaled (This->width () -2*shadow_width, SHADOW_WIDTH));     painter.drawpixmap (Shadow_width, This->height ()-shadow_width, This->width () -2* Shadow_width, Shadow_width, pixmaps[3].scaled (This->width () -2*shadow_width, shadow_width));}       write a common class, if the window is to implement the shadow effect directly inherit on the line! Shadow_width is the shadow border of the pixel, can be adjusted according to their own needs free!      can also use Qgraphicsdropshadoweffect to achieve ...    more reference: QT Shadow border .   Front on the window shadow has written a blog, using the idea of nine Gongge to achieve, in my opinion, generally use the program can be achieved as far as possible do not use the picture instead of(On the premise of ensuring efficiency), share some of my little insights today!      first look at the effect:      the window shadow arbitrary adjustment, including shadow pixels, whether rounded corners and so on.      Direct code:  void dropshadowwidget::p aintevent (qpaintevent *event) {     qpainterpath path;    path.setfillrule (Qt::windingfill);     Path.addrect (This->width () -20, This->height () -20);      qpainter painter ( this);     painter.setrenderhint (qpainter::antialiasing, True);     Painter.fillpath (Path, Qbrush (Qt::white)),      qcolor color (0, 0, 0,);     for (int i=0; i<10; i++)     {         Qpainterpath path;        path.setfillrule (Qt::WindingFill);         path.addrect (10-i, 10-i, This->width ()-(10-i) * *, This->height ()-( 10-I);          color.setalpha (150-qsqrt (i) *50);         painter.setpen (color);         painter.drawpath (path);     }}     Remember to add this line of code: SetAttribute (Qt::wa_translucentbackground). Guaranteed not to be partially transparent on the drawing, there are side effects in some QT versions of this line of code, such as: problems such as the loss of focus of the form's subcomponents after minimizing. You can see this bug in Qt (Widget with Qt::framelesswindowhint and qt::wa_translucentbackground stops painting after minimize/ Restore).      Maybe someone will come across, because I have been using the VS Integrated Qt5 plugin (non-OpenGL version), there has been this problem, looking for all aspects of data without fruit (really long, do not exaggerate that the big six months should be there). Finally changed OpenGL version of incredibly good ... The solution to the problem is too bizarre, really makes people laugh and cry. In this demerit, hope to yimeimei helpful.          Adding a shadow to a subassembly is simple, using the following method: Qgraphicsdropshadoweffect *shadow_effect = New Qgraphicsdropshadoweffect (this); Shadow_effect->setoffset ( -5, 5); Shadow_effect->setcolor (Qt::gray); Shadow_effect->setblurradius (8); Network_group_box->setgraphicseffect (shadow_effect);  effect is as follows:

Qt Shadow border (GO)

Related Article

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.