代码之家  ›  专栏  ›  技术社区  ›  smerlin

QT4:圆角透明窗

  •  21
  • smerlin  · 技术社区  · 16 年前

    如何创建具有圆形边框(无标准边框)的部分透明窗口?

    (我用过 Qt::FramelessWindowHint 禁用标准边框的步骤)

    border-radius opacity 似乎对窗口没有任何影响,它只对封闭小部件的子项有效。

    我的第二个想法是使窗户完全透明(带有 setWindowOpacity 然后将我的所有其他小部件分组到该小部件中。但从那时起,这就不起作用了 设置窗口不透明度 也会影响所有儿童(我还没有找到改变这种行为的方法)。

    还有我能想到的任何使外部窗口透明的方法(比如样式表) 不透明度 )工作不正常(我只得到一个黑盒子而不是一个透明的窗口)

    3 回复  |  直到 10 年前
        1
  •  17
  •   ctd vtortola    16 年前

    我有一个类似的问题,我想在顶级小部件上绘制,但只显示绘制的部分。setWindowOpacity更改了绘制部分的不透明度,这是我不想要的。

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

    更改了小部件的不透明度,但没有绘制的部分。我只是试着按一下按钮,那也显示出完全不透明。那么你呢 应该 能够以您喜欢的方式显示其他孩子。

        2
  •  7
  •   Tarod    10 年前

    我认为您应该使用widget掩码,如下面的Qt示例所示:

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

    我想你会在里面找到你想要的!

        3
  •  2
  •   Voland    15 年前
     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));
    }