Qt之图像处理

QPainter

绘制基本图形的类

  • 一次性填充绘制
cpp 复制代码
QVector<QPoint> drawPointsGray;//存储灰色的点
//...将所有要画灰色的点加入该数组中
painter.setPen(Qt::gray);//设置颜色
painter.drawPoints(drawPointsGray.data(), drawPointsGray.size());//测试

QImage

  • 根据地址直接加载
cpp 复制代码
image.load("当前项目所在路径下的图片名");
  • 将图像转换为二进制数据
cpp 复制代码
    QByteArray byteArray;
    QBuffer buffer(&byteArray);
    buffer.open(QIODevice::WriteOnly);
    image.save(&buffer, "PNG"); // 这里可以指定其他格式,如 "JPG"、"BMP" 等
    buffer.close();
  • 将二进制数据转换为图像
cpp 复制代码
image.loadFromData(const char*);

QLabel

  • 起始点
cpp 复制代码
label.pos();
  • 宽度和高度
cpp 复制代码
label.width();
label.height();
  • 放大缩小处理
cpp 复制代码
//将图片按比例缩放    
QImage image1 = image.scaled(this->width() * 比例系数, this->height() *  比例系数);
//将缩放后的图片转换为QPixmap对象
QPixmap pixmap = QPixmap::fromImage(image1);
//根据图像大小的变化,调整当前图像的尺寸
double width= this->pixmap()->width()-pixmap.width();
double height= this->pixmap()->height()-pixmap.height();
this->setGeometry(this->x()+width/2,this->y()+height/2,this->width()* 比例系数,this->height()* 比例系数);
this->setPixmap(pixmap);
//根据图像变化更新并设置物理参数
change_physics_resolution_x = physics_width /this->width();//现实场景宽度/图像宽度
change_physics_resolution_y = physics_height /this->height();//现实场景高度/图像高度
setPhysics( this->width(), this->height(), change_physics_resolution_x,change_physics_resolution_y);
//更新图像上绘制的对象大小
graphics_view->setGeometry(0,0,this->width(),this->height());
triangle_view->setGeometry(0,0,this->width(),this->height());

QPainterPath

用于绘制图形路径的类。它允许你定义一系列线条和曲线,然后可以在画布上绘制这些路径。

  • 绘制贝塞尔曲线
cpp 复制代码
QPainterPath path;
path.moveTo(src_point);//设置路径起始点
path.quadTo(curve.control_point_1, dst_point);//设置两个控制点
QPen pen(Qt::blue);
pen.setWidth(4); // 设置线条宽度为5
QGraphicsPathItem* pathItem = scene_->addPath(path, pen);//将路径添加进入scene_
pathItem->setZValue(1);//将场景中的路径项设置为1
相关推荐
侃侃_天下2 天前
最终的信号类
开发语言·c++·算法
echoarts2 天前
Rayon Rust中的数据并行库入门教程
开发语言·其他·算法·rust
Aomnitrix2 天前
知识管理新范式——cpolar+Wiki.js打造企业级分布式知识库
开发语言·javascript·分布式
每天回答3个问题2 天前
UE5C++编译遇到MSB3073
开发语言·c++·ue5
伍哥的传说2 天前
Vite Plugin PWA – 零配置构建现代渐进式Web应用
开发语言·前端·javascript·web app·pwa·service worker·workbox
小莞尔2 天前
【51单片机】【protues仿真】 基于51单片机八路抢答器系统
c语言·开发语言·单片机·嵌入式硬件·51单片机
我是菜鸟0713号2 天前
Qt 中 OPC UA 通讯实战
开发语言·qt
JCBP_2 天前
QT(4)
开发语言·汇编·c++·qt·算法
Brookty2 天前
【JavaEE】线程安全-内存可见性、指令全排序
java·开发语言·后端·java-ee·线程安全·内存可见性·指令重排序
百锦再2 天前
[特殊字符] Python在CentOS系统执行深度指南
开发语言·python·plotly·django·centos·virtualenv·pygame