You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
senjing/src/Qt_1.cpp

44 lines
1.4 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

QPixmap pixmap("image.png"); // 加载图像
QPainter painter(&pixmap); // 创建绘图对象
// 指定矩形范围这里以左上角为起点宽高为100像素的矩形为例
int x = 0;
int y = 0;
int width = 100;
int height = 100;
// 创建画笔对象这里使用Qt中预定义的红色
QPen pen(Qt::red);
pen.setWidth(2); // 设置边框宽度
// 绘制矩形框
painter.setPen(pen);
painter.drawRect(x, y, width, height);
// 显示图像
QLabel label;
label.setPixmap(pixmap);
label.show();
// 指定矩形范围这里以左上角为起点宽高为100像素的矩形为例
int x = 0;
int y = 0;
int width = 100;
int height = 100;
// 获取像素大小
QSize size = pixmap.size(); // 获取图像尺寸
double pixelSizeX = 1.0 / size.width(); // 计算x方向每个像素在实际场景中所代表的长度
double pixelSizeY = 1.0 / size.height(); // 计算y方向每个像素在实际场景中所代表的长度
// 计算实际场景中的长度
int pixelCount = width * height; // 计算指定范围内的像素数量
double lengthX = pixelCount * pixelSizeX; // 计算x方向实际场景中的长度
double lengthY = pixelCount * pixelSizeY; // 计算y方向实际场景中的长度
qDebug() << "实际场景中的长度x方向" << lengthX;
qDebug() << "实际场景中的长度y方向" << lengthY;