• 透明窗体和控件


    调用函数设置窗体透明度:

    setWindowOpacity(x);   x(0-1)可以为小数  0.1 0.2 0.3等

    • x=0 时完全透明
    • k=1时不透明
        setWindowOpacity(0.5);

     当有控件时,控件也变透明,在ui界面中添加两个按钮

     使窗体透明但控件不透明

    setWindowFlag(Qt::FramelessWindowHint);去除边框

    setAttribute(Qt::WA_translucentBackground);使窗口透明

    但这种方式窗口是全透明

    注意:setAttribute需要配合setWindowFlag一起使用

    1. setWindowFlag(Qt::FramelessWindowHint);
    2. setAttribute(Qt::WA_TranslucentBackground);

     实现窗口半透明:

    使用重绘事件对窗口进行绘制

    在widget.h中添加事件:

        void paintEvent(QPaintEvent *event);//绘图事件

    构造函数中添加:

    1. setWindowFlag(Qt::FramelessWindowHint);
    2. setAttribute(Qt::WA_TranslucentBackground);

    在widget.cpp中实现绘图事件

    • fillRect()指定区域设置属性
    • rect()获取内部矩形
    1. void Widget::paintEvent(QPaintEvent *event)//绘图事件
    2. {
    3. QPainter painter(this);
    4. painter.fillRect(rect(),QColor(255,255,255,200));
    5. }

    当有背景图片时:

    创建一个新项目

     添加绘画事件;

    1. protected:
    2. void paintEvent(QPaintEvent *event);//绘图事件

    实现绘画事件:

    1. void Widget::paintEvent(QPaintEvent *event)//绘图事件
    2. {
    3. QPainter painter(this);//创建一个画家
    4. QPixmap pix(":/image/img.jpg");
    5. painter.drawPixmap(0,0,pix);//绘制图片
    6. this->resize(pix.size());//设置窗口大小
    7. setWindowOpacity(0.5);
    8. }

     

     使用样式表设置控件透明度

     background-color:rgba(255,255,255,x)x代表透明度  0为完全透明 255为不透明

    通过函数setStyleSheet()设置样式表

        ui->pushButton->setStyleSheet("background-color:rgba(103,182,255,50)");

    1. #pushButton
    2. {
    3. background-color: rgba(103, 182, 255,50);
    4. }

    注意对主窗口使用 background-color:rgba(255,255,255,x)

    窗口的底色为黑色,数值越小越趋近黑色,所有不会去使用 background-color:rgba(255,255,255,x)设置主窗口的透明度。

    1. QWidget
    2. {
    3. background-color: rgba(24, 174, 255,0);
    4. }

  • 相关阅读:
    从零玩转系列之微信支付实战PC端支付微信回调接口搭建
    Java : 类加载和双亲委派模型
    【新版】系统架构设计师 - 案例分析 - 架构设计<Web架构>
    【程序猿保健】ShaderJoy —— 拉力带教程
    RabbitMQ快速入门
    Xshell如何连接虚拟机
    怎样减少报表开发中的存储过程
    js的节流和防抖详解
    多线程的学习中篇上
    二叉搜索树+二叉进阶oj
  • 原文地址:https://blog.csdn.net/qq_45303986/article/details/127905367