效果:
int resolutionWidth = m_resolution.width();
int resolutionHeight = m_resolution.height();
QPixmap pixmap(resolutionWidth, resolutionHeight);
pixmap.fill(Qt::transparent);
QPainter painter(&pixmap);
painter.setPen(QPen(Qt::red, 2)); // 设置画笔颜色为红色,宽度为2
// 绘制垂直线
painter.drawLine(resolutionWidth / 2, 0, resolutionWidth / 2, resolutionHeight);
// 绘制水平线
painter.drawLine(0, resolutionHeight / 2, resolutionWidth, resolutionHeight / 2);
int centerX = resolutionWidth / 2;
int centerY = resolutionHeight / 2;
// 在十字中间添加值
QString centerText = "Center";
QRectF centerRect(centerX + 20, centerY , 40, 20);
painter.drawText(centerRect, Qt::AlignCenter, centerText);
// // 绘制量尺
int rulerLength = m_resolution.width(); // 量尺长度
int tickSpacing = 10; // 刻度间距
// 绘制水平中心线上的量尺
int rulerStartXHorizontal = centerX - rulerLength / 2; // 水平量尺起始位置X坐标
int rulerStartYHorizontal = centerY; // 水平量尺起始位置Y坐标
for (int i = 0; i <= rulerLength; i += tickSpacing) {
int tickHeight = (i % (tickSpacing * 5) == 0) ? 20 : 10; // 长刻度的高度为20,短刻度的高度为10
painter.drawLine(rulerStartXHorizontal + i , rulerStartYHorizontal , rulerStartXHorizontal + i, rulerStartYHorizontal - tickHeight ); // 绘制刻度线
}
// 绘制垂直中心线上的量尺
int rulerStartXVertical = centerX; // 垂直量尺起始位置X坐标
int rulerStartYVertical = centerY + rulerLength / 2; // 垂直量尺起始位置Y坐标
for (int i = 0; i <= rulerLength; i += tickSpacing) {
int tickWidth = (i % (tickSpacing * 5) == 0) ? 20 : 10; // 长刻度的宽度为20,短刻度的宽度为10
painter.drawLine(rulerStartXVertical, rulerStartYVertical - i, rulerStartXVertical - tickWidth, rulerStartYVertical - i); // 绘制刻度线
}
ui->workWidget->updatePixmap1(pixmap); // 更新 workWidget 上显示的图像