当前位置:网站首页>Qt实战案例(54)——利用QPixmap设计图片透明度
Qt实战案例(54)——利用QPixmap设计图片透明度
2022-07-31 15:28:00 【wendy_ya】
一、项目介绍
本文介绍利用QPixmap设计图片透明度,可以看到拖动下方进度条,用于控制上方图片的透明度。
二、项目基本配置
新建一个Qt案例,项目名称为“TransparencyTest”,基类选择“QWidget”,点击选中创建UI界面复选框,完成项目创建。
三、UI界面设置
UI界面如下:
| 序号 | 名称 | 类型 | 属性 |
|---|---|---|---|
| ① | label_photo | QLabel | / |
| ② | slider | QSlider | minimum:0;maximum:255; |
四、主程序实现
4.1 widget.h头文件
头文件中需要声明滑动条移动槽函数:
右键——>滑动条——>sliderMoved:
我这里是:
private slots:
void on_Slider_sliderMoved(int position);
4.2 widget.cpp源文件
源文件中首先在构造函数中设置背景label的图片,然后设置label位置,设置滑动条初始位置位于最右侧:
//设置窗口大小
setFixedSize(800,600);
//设置背景label的图片
QPixmap pix(":/test.jpg");
ui->label_photo->setPixmap(pix);
//设置lable位置
ui->label_photo->setScaledContents(true);
ui->label_photo->setGeometry(10,10,200,150);
ui->label_photo->raise();
ui->label_photo->show();
//设置初始滑动条位置在最右端
ui->Slider->setValue(255);
定义滑动条滑动槽函数:
void Widget::on_Slider_sliderMoved(int position)
{
//设置新的图片的透明度
QPixmap pix1(":/test.jpg");
QPixmap temp(pix1.size());
temp.fill(Qt::transparent);
QPainter p1(&temp);
p1.setCompositionMode(QPainter::CompositionMode_Source);
p1.drawPixmap(0, 0, pix1);
p1.setCompositionMode(QPainter::CompositionMode_DestinationIn);
//根据QColor中第四个参数设置透明度,此处position的取值范围是0~255
p1.fillRect(temp.rect(), QColor(0, 0, 0, position));
p1.end();
pix1 = temp;
ui->label_photo->setPixmap(pix1);
}
五、效果演示
完整效果如下:
如果没有看懂的话,完整代码可以参考:https://download.csdn.net/download/didi_ya/86268287
ok,以上便是本文的全部内容了,如果对你有所帮助,记得点个赞哟~
边栏推荐
- The use of border controls
- json到底是什么(c# json)
- RecyclerView的高效使用第一节
- Efficient use of RecyclerView Section 1
- Ubantu专题4:xshell、xftp连接接虚拟机以及设置xshell复制粘贴快捷键
- Deployment application life cycle and Pod health check
- Emmet syntax
- Implement anti-shake and throttling functions
- org.apache.jasperException(could not initialize class org)
- leetcode303场周赛复盘
猜你喜欢

女性服务社群产品设计

MySQL的相关问题

Ubantu project 4: xshell, XFTP connected the virtual machine and set xshell copy and paste the shortcut

MySQL基础篇【单行函数】

Synchronized and volatile interview brief summary

radiobutton的使用

三、数组

The use of button controls

Female service community product design

【Meetup预告】OpenMLDB+OneFlow:链接特征工程到模型训练,加速机器学习模型开发
随机推荐
Delete the disk in good condition (recovery partition)
Word table to Excel
JVM parameter analysis Xmx, Xms, Xmn, NewRatio, SurvivorRatio, PermSize, PrintGC "recommended collection"
Ubantu project 4: xshell, XFTP connected the virtual machine and set xshell copy and paste the shortcut
abaqus find contact pairs报错:surface name is already in use
TRACE32——常用操作
JVM参数解析 Xmx、Xms、Xmn、NewRatio、SurvivorRatio、PermSize、PrintGC「建议收藏」
ML.NET相关资源整理
ES6 类
R language moves time series data forward or backward (custom lag or lead period): use the lag function in the dplyr package to move the time series data forward by one day (set the parameter n to a p
Ubantu专题5:设置静态ip地址
Insert into data table to insert data
【MySQL】Mysql范式及外键作用
Kubernetes common commands
格林美瑞交所IPO:募资3.8亿美元 更多中国企业将赴欧洲上市
Why is the field of hacking almost filled with boys?
MySQL基础篇【单行函数】
Use of radiobutton
mysql black window ~ build database and build table
【CUDA学习笔记】初识CUDA