当前位置:网站首页>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,以上便是本文的全部内容了,如果对你有所帮助,记得点个赞哟~
边栏推荐
- 安装Xshell并使用其进行Ymodem协议的串口传输
- 【Meetup预告】OpenMLDB+OneFlow:链接特征工程到模型训练,加速机器学习模型开发
- 自动化测试如何创造业务价值?
- The use of button controls
- TRACE32——基于SNOOPer的变量记录
- TRACE32——常用操作
- 【CUDA学习笔记】初识CUDA
- 实现防抖与节流函数
- R language test whether the sample conforms to normality (test whether the sample comes from a normally distributed population): shapiro.test function tests whether the sample conforms to the normal d
- Codeforces Round #796 (Div. 2)(A-D)
猜你喜欢
随机推荐
Female service community product design
TRACE32 - C source code association
华医网冲刺港股:5个月亏2976万 红杉与姚文彬是股东
更新数据表update
Selenium自动化中无头浏览器的应用
「秋招系列」MySQL面试核心25问(附答案)
女性服务社群产品设计
TextBlock控件入门基础工具使用用法,取上法入门
工程水文学试卷
How useful is four-quadrant time management?
Grafana安装后web打开报错
网银被盗?这篇文章告诉你如何安全使用网银
After Grafana is installed, the web opens and reports an error
RecyclerView高效使用第二节
Jmeter常用的十大组件
Kubernetes principle analysis and practical application manual, too complete
Delete table data or clear table
org.apache.jasperException(could not initialize class org)
国内市场上的BI软件,到底有啥区别
Public Key Retrieval is not allowed error solution when DBeaver connects to MySQL 8.x









