QT4: Transparent Window with rounded corners

前端 未结 3 1777
你的背包
你的背包 2020-12-03 01:40

How can I create a partially transparent window with rounded borders (no standard borders)?

(I used Qt::FramelessWindowHint to disable standard border

相关标签:
3条回答
  • 2020-12-03 02:03

    I had a similar problem where I wanted to paint on a toplevel widget and have only the painted part appear. setWindowOpacity changed the opacity of the painted part, which I didn't want.

    this->setAttribute(Qt::WA_TranslucentBackground, true);
    

    changed the opacity of the widget without the painted part. I just tried tossing on a button, and that also displays perfectly opaque. So you should be able to display other children however you like.

    0 讨论(0)
  • 2020-12-03 02:08

    I think you should use a widget masks, as seen in the following example from Qt :

    http://doc.qt.io/qt-5/qtwidgets-widgets-shapedclock-example.html

    I think you'll find what you're looking for in it !

    Hope this helps a bit!

    0 讨论(0)
  • 2020-12-03 02:18
     void MainForm::resizeEvent(QResizeEvent * /* event */)
    {
        QImage image(this->size(), QImage::Format_Mono);
        image.fill(0);
    
        if(!this->isFullScreen() && !this->isMaximized())
        {
            image.setPixel(0, 0, 1); image.setPixel(1, 0, 1); image.setPixel(2, 0, 1); image.setPixel(3, 0, 1);
            image.setPixel(0, 1, 1); image.setPixel(1, 1, 1);
            image.setPixel(0, 2, 1);
            image.setPixel(0, 3, 1);
    
            image.setPixel(width() - 4, 0, 1); image.setPixel(width() - 3, 0, 1); image.setPixel(width() - 2, 0, 1); image.setPixel(width() - 1, 0, 1);
                                                                                  image.setPixel(width() - 2, 1, 1); image.setPixel(width() - 1, 1, 1);
                                                                                                                     image.setPixel(width() - 1, 2, 1);
                                                                                                                     image.setPixel(width() - 1, 3, 1);
    
            image.setPixel(0, height() - 4, 1);
            image.setPixel(0, height() - 3, 1);
            image.setPixel(0, height() - 2, 1); image.setPixel(1, height() - 2, 1);
            image.setPixel(0, height() - 1, 1); image.setPixel(1, height() - 1, 1); image.setPixel(2, height() - 1, 1); image.setPixel(3, height() - 1, 1);
    
                                                                                                                                                      image.setPixel(width() - 1, height() - 4, 1);
                                                                                                                                                      image.setPixel(width() - 1, height() - 3, 1);
                                                                                                        image.setPixel(width() - 2, height() - 2, 1); image.setPixel(width() - 1, height() - 2, 1);
            image.setPixel(width() - 4, height() - 1, 1); image.setPixel(width() - 3, height() - 1, 1); image.setPixel(width() - 2, height() - 1, 1); image.setPixel(width() - 1, height() - 1, 1);
        }
        this->setMask(QPixmap::fromImage(image));
    }
    
    0 讨论(0)
提交回复
热议问题