用qt写一个可以在图片上任意画有限条线,且在各条线起点和终点旁边分别显示像素坐标的代码,坐标的原点为图像的左上角,头文件和源文件分开
时间: 2023-09-27 14:05:26 浏览: 110
### 回答1:
头文件:
```cpp
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QImage>
#include <QPainter>
#include <QMouseEvent>
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
private:
QImage image;
QPainter painter;
QPoint startPt, endPt;
QList<QLine> lines;
protected:
void paintEvent(QPaintEvent *event);
void mousePressEvent(QMouseEvent *event);
void mouseMoveEvent(QMouseEvent *event);
void mouseReleaseEvent(QMouseEvent *event);
};
#endif // MAINWINDOW_H
```
源文件:
```cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
image = QImage(640, 480, QImage::Format_RGB32);
image.fill(Qt::white);
}
MainWindow::~MainWindow()
{
}
void MainWindow::paintEvent(QPaintEvent *event)
{
Q_UNUSED(event);
painter.begin(this);
painter.drawImage(0, 0, image);
painter.end();
}
void MainWindow::mousePressEvent(QMouseEvent *event)
{
startPt = event->pos();
}
void MainWindow::mouseMoveEvent(QMouseEvent *event)
{
endPt = event->pos();
update();
}
void MainWindow::mouseReleaseEvent(QMouseEvent *event)
{
endPt = event->pos();
lines << QLine(startPt, endPt);
update();
}
```
```cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
image = QImage(640, 480, QImage::Format_RGB32);
image.fill(Qt::white);
}
MainWindow::~MainWindow()
{
}
void MainWindow::paintEvent(QPaintEvent *event)
{
Q_UNUSED(event);
painter.begin(&image);
painter.setPen(Qt::black);
for (const QLine &line : lines) {
painter.drawLine(line);
painter.drawText(line.p1() + QPoint(5, -5), QString("(%1, %2)").arg(line.x1()).arg(line.y1()));
painter.drawText(line.p2() + QPoint(5, -5), QString("(%1, %2)").arg(line.x2()).arg(line.y2()));
}
painter.setPen(Qt::red);
painter.drawLine(startPt, endPt);
painter.drawText(startPt + QPoint(5, -5), QString("(%1, %2)").arg(startPt.x()).arg(startPt.y()));
painter.drawText(endPt + QPoint(5, -5), QString("(%1, %2)").arg(endPt.x()).arg(endPt.y()));
painter.end();
QMainWindow::paintEvent(event);
}
void MainWindow::mousePressEvent(QMouseEvent *event)
{
startPt = event->pos();
}
void MainWindow::mouseMoveEvent(QMouseEvent *event)
{
endPt = event->pos();
update();
}
void MainWindow::mouseReleaseEvent(QMouseEvent *event)
{
endPt = event->pos();
lines << QLine(startPt, endPt);
update();
}
```
### 回答2:
头文件(linepainter.h):
#ifndef LINEPAINTER_H
#define LINEPAINTER_H
#include <QWidget>
#include <QPoint>
#include <QVector>
class LinePainter : public QWidget
{
Q_OBJECT
public:
explicit LinePainter(QWidget *parent = nullptr);
void addLine(const QPoint &start, const QPoint &end);
protected:
void paintEvent(QPaintEvent *event) override;
private:
QVector<QPair<QPoint, QPoint>> m_lines;
};
#endif // LINEPAINTER_H
源文件(linepainter.cpp):
#include "linepainter.h"
#include <QPainter>
LinePainter::LinePainter(QWidget *parent)
: QWidget(parent)
{
}
void LinePainter::addLine(const QPoint &start, const QPoint &end)
{
m_lines.append(qMakePair(start, end));
update(); // 更新绘制
}
void LinePainter::paintEvent(QPaintEvent *event)
{
QWidget::paintEvent(event);
QPainter painter(this);
painter.setRenderHint(QPainter::Antialiasing); // 抗锯齿
// 绘制所有线条
for (const auto &line : m_lines) {
painter.drawLine(line.first, line.second);
}
// 绘制线条起点和终点的像素坐标
QFont font;
font.setPixelSize(12);
painter.setFont(font);
painter.setPen(Qt::red);
for (const auto &line : m_lines) {
painter.drawText(line.first, QString("(%1,%2)").arg(line.first.x()).arg(line.first.y()));
painter.drawText(line.second, QString("(%1,%2)").arg(line.second.x()).arg(line.second.y()));
}
}
使用方法示例:
#include "linepainter.h"
#include <QtWidgets>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QWidget window;
QVBoxLayout layout(&window);
LinePainter painter;
layout.addWidget(&painter);
window.setLayout(&layout);
window.show();
// 添加线条
painter.addLine(QPoint(50, 100), QPoint(200, 150));
painter.addLine(QPoint(100, 200), QPoint(300, 250));
return a.exec();
}
运行上述示例代码,可以在窗口上绘制两条线,并在每条线的起点和终点旁边显示像素坐标。
### 回答3:
头文件(imagepainter.h):
```cpp
#ifndef IMAGE_PAINTER_H
#define IMAGE_PAINTER_H
#include <QImage>
#include <QPainter>
#include <QWidget>
#include <QMouseEvent>
class ImagePainter : public QWidget
{
Q_OBJECT
public:
explicit ImagePainter(QWidget *parent = nullptr);
void setImage(const QImage &image);
protected:
void paintEvent(QPaintEvent *event) override;
void mousePressEvent(QMouseEvent *event) override;
void mouseReleaseEvent(QMouseEvent *event) override;
private:
QImage m_image;
QVector<QLine> m_lines;
QPoint m_startPoint;
};
#endif // IMAGE_PAINTER_H
```
源文件(imagepainter.cpp):
```cpp
#include "imagepainter.h"
ImagePainter::ImagePainter(QWidget *parent) : QWidget(parent)
{
}
void ImagePainter::setImage(const QImage &image)
{
m_image = image;
update();
}
void ImagePainter::paintEvent(QPaintEvent *event)
{
QPainter painter(this);
painter.drawImage(0, 0, m_image);
painter.setPen(Qt::red);
for (const QLine &line : m_lines) {
painter.drawLine(line);
}
}
void ImagePainter::mousePressEvent(QMouseEvent *event)
{
m_startPoint = event->pos();
event->accept();
}
void ImagePainter::mouseReleaseEvent(QMouseEvent *event)
{
QPoint endPoint = event->pos();
QLine line(m_startPoint, endPoint);
m_lines.append(line);
event->accept();
update();
}
```
使用示例(main.cpp):
```cpp
#include <QApplication>
#include "imagepainter.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QImage image(":/images/example.jpg");
ImagePainter painter;
painter.setImage(image);
painter.show();
return a.exec();
}
```
上述代码定义了一个名为ImagePainter的自定义QWidget,并在其上实现了图片绘制功能。通过mousePressEvent和mouseReleaseEvent函数,可以在图片上绘制任意数量的线条。每绘制一条线条后,会在该线条的起点和终点旁边显示像素坐标。坐标的原点为图像的左上角。在使用时,只需将所需绘制的图片路径传递给setImage函数即可。
阅读全文