Qt窗口特殊效果

Qt窗口特殊效果

  • 窗口直接透明

setWindowOpacity(0.1);

  • 部件不透明,窗体背景完全透明,Windows下需要配合无边框风格
    1
    2
    setWindowFlags(Qt::FramelessWindowHint);
    setAttribute(Qt::WA_TranslucentBackground);
  • 只设置部件透明
    1
    2
    3
    QGraphicsOpacityEffect * opacityEffect = new QGraphicsOpacityEffect;
    opacityEffect->setOpacity(0.1);
    ui->label->setGraphicsEffect(opacityEffect);
  • 阴影效果
    1
    2
    3
    4
    5
    6
    7
    8
    9
        

    ```cpp
    QGraphicsDropShadowEffect* shadowEffect = new QGraphicsDropShadowEffect;
    //阴影色,透明度
    shadowEffect->setColor(QColor(100,100,100));
    shadowEffect->setBlurRadius(20);//阴影模糊半径
    shadowEffect->setOffset(20);//阴影偏移值
    ui->label->setGraphicsEffect(shadowEffect);
  • 实现半透明
    1
    2
    3
    4
    5
    6
    //重写paintEvent
    void MainWindow::paintEvent(QPaintEvent* event)
    { //实现半透明
    QPainter painter(this);
    painter.fillRect(rect(),QColor(255,255,255,256));
    }
  • 设置不规则窗体形状
  1. 找一个PNG,加到资源文件
  2.  pixmap.load(":/flower.png");
     resize(pixmap.size());
     setMask(pixmap.mask());//设置遮罩```
    
  3. 重写paintEvent
    1
    2
    3
    4
    5
    void Widget::paintEvent(QPaintEvent *event)
    {
    QPainter painter(this);
    painter.drawPixmap(0,0,QPixmap(":/flower.png"));
    }
作者

步步为营

发布于

2024-05-08

更新于

2025-03-15

许可协议