The self-QT irregular shaped windows and buttons (pro-test)

See a lot of software, especially gaming software, there are some cool interface, have been thinking, how did they do it? That obviously is a picture ah.

I have a big world QT can not do it? of course not!

Now: Please prepare a png image with transparent areas, oh. There is a header file Join #include<QBitmap> , to see behind you understand it, or will be error oh.

First look at the custom shaped windows bar code:


  
  
  1. Widget::Widget(QWidget *parent)
  2. : QWidget(parent)
  3. {
  4. this->setWindowFlags(Qt::FramelessWindowHint|Qt::WindowMinimizeButtonHint);
  5. this->setAttribute(Qt::WA_TranslucentBackground);
  6. pix.load( ":/new/prefix1/201409091359143092.png");
  7. this->resize(pix.size());
  8. }
  9. void Widget::paintEvent(QPaintEvent *e)
  10. {
  11. QPainter painter(this);
  12. painter.drawPixmap( 0, 0,pix);
  13. }

Here wrote paintevent sure to draw inside, be sure to call the constructor also resize.

Then take a look at the interface of it.

This interface is complex enough, right? Who else! Oh, you can add controls above. Here more than just show pictures, careful research can be seen, this form is visible area, the other invisible, not the form, this is the real irregular.

Of course, since removed the title bar, so moving event of the form, you need to rewrite their own, seemingly too hard.

There is also a

Maybe you do not like it, you want to setstylesheet way, then look at the following code oh.


  
  
  1. #include <QStyleOption>
  2. Widget::Widget(QWidget *parent)
  3. : QWidget(parent)
  4. {
  5. this->resize( 400, 400);
  6. setWindowOpacity ( 1);
  7. setWindowFlags(Qt::FramelessWindowHint);
  8. setAttribute(Qt::WA_TranslucentBackground);
  9. this->setStyleSheet( "background-image: url(:/new/prefix1/201409091359143092.png)");
  10. }
  11. void Widget::paintEvent(QPaintEvent *e)
  12. {
  13. QStyleOption opt;
  14. opt.initFrom( this);
  15. QPainter p(this);
  16. style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this );
  17. QWidget::paintEvent(e);
  18. }

This is also possible.


Next is irregular button


  
  
  1. QPixmap pix;
  2. pix.load( ":/new/prefix1/111.png");
  3. ui->pushButton->setFixedSize(pix.size());
  4. ui->pushButton->setMask(pix.mask());
  5. ui->pushButton->setStyleSheet( "background-image: url(:/new/prefix1/111.png)");
This code say about the principle. mask actually get the alpha channel information is then displayed, which causes a problem in the picture when displayed, will change, so we give this button to specify about the background, the background and it overlaps with, it shows the normal . As for why the words of the form need not do so, this has not been studied ......

Renderings:

This button is wonderful enough of it, surrounded click, the middle can not click. But then, your button image can not be too complex, such as the map above form will not work, the problem will appear Caton or not displayed.

Well, the end.

Here comes suddenly think it is a direct function setmask acceptable type of bitmap images, but it seems not.

For example, like this:


  
  
  1. QBitmap bit;
  2. bit.load( ":/new/prefix1/222.bmp");
  3. ui->pushButton->setMask(bit);
  4. ui->pushButton->setStyleSheet( "background-image: url(:/new/prefix1/222.bmp)");

This code causes a color change problem is a picture, but then I try various methods do not work, this code will lead to even click a button when Caton, currently do not know why, do not try enough.

Wonderful effect is as follows:

Published 42 original articles · won praise 148 · views 410 000 +

Guess you like

Origin blog.csdn.net/baidu_37503452/article/details/104253189