简体   繁体   English

QT4:带圆角的透明窗口

[英]QT4: Transparent Window with rounded corners

How can I create a partially transparent window with rounded borders (no standard borders)? 如何创建具有圆形边框(无标准边框)的部分透明窗口?

(I used Qt::FramelessWindowHint to disable standard borders) (我使用Qt::FramelessWindowHint来禁用标准边框)

I tried stylesheets, but border-radius and opacity doesn't seem to have any effect on the window, it only works on children of the enclosing widget. 我尝试过样式表,但是border-radiusopacity似乎对窗口没有任何影响,它只适用于封闭小部件的子代。

My second idea was to make the window fully transparent (with setWindowOpacity ), and then add an additional widget with rounded corners (since border-radius works on the children), and then group all my other widgets into that widget. 我的第二个想法是使窗口完全透明(使用setWindowOpacity ),然后添加一个带圆角的附加小部件(因为border-radius适用于子级),然后将我的所有其他小部件分组到该小部件中。 But that does not work since setWindowOpacity affects all children as well (I haven't found a way to change this behaviour). 但这不起作用,因为setWindowOpacity影响所有孩子(我还没有找到改变这种行为的方法)。

And any ways to make the outer window transparent I could think of (like stylesheets opacity ) don't work properly (I only get a black box instead of a transparent window) 任何使我想到的外窗透明的方法(如样式表opacity )都不能正常工作(我只得到一个黑盒子而不是透明窗口)

Any help would be highly appreciated. 任何帮助将受到高度赞赏。

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. setWindowOpacity更改了绘制部分的不透明度,这是我不想要的。

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. 所以你应该能够展示你喜欢的其他孩子。

I think you should use a widget masks, as seen in the following example from Qt : 我认为您应该使用小部件掩码,如以下Qt中的示例所示:

http://doc.qt.io/qt-5/qtwidgets-widgets-shapedclock-example.html 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! 希望这个对你有帮助!

 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));
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM