Qt在QLabel显示图片并绘制矩形框,顶点可修改并且能够拖动
时间: 2024-05-05 19:21:48 浏览: 316
QT 在QLabel上加载图片 并在其上绘制矩形
可以使用Qt的QPainter和QGraphicsView来实现这个功能。
首先,创建一个继承自QGraphicsView的类来显示图片和矩形框:
```cpp
class ImageView : public QGraphicsView
{
public:
ImageView(QWidget* parent = nullptr);
void setImage(const QPixmap& image);
void setRect(const QRectF& rect);
protected:
void mouseMoveEvent(QMouseEvent* event) override;
void mousePressEvent(QMouseEvent* event) override;
void mouseReleaseEvent(QMouseEvent* event) override;
void wheelEvent(QWheelEvent* event) override;
void drawBackground(QPainter* painter, const QRectF& rect) override;
private:
QGraphicsPixmapItem* m_pixmapItem;
QGraphicsRectItem* m_rectItem;
QPointF m_lastPos;
};
```
在构造函数中,我们创建了一个QGraphicsPixmapItem用于显示图片,和一个QGraphicsRectItem用于显示矩形框。在setImage和setRect函数中,我们分别设置了图片和矩形框的位置和大小。
在mouseMoveEvent、mousePressEvent和mouseReleaseEvent函数中,我们实现了矩形框顶点的拖动功能。在wheelEvent函数中,我们实现了缩放图片的功能。在drawBackground函数中,我们绘制了网格线以方便观察。
下面是实现代码:
```cpp
ImageView::ImageView(QWidget* parent)
: QGraphicsView(parent)
{
setDragMode(QGraphicsView::RubberBandDrag);
setRenderHint(QPainter::Antialiasing);
setRenderHint(QPainter::SmoothPixmapTransform);
m_pixmapItem = new QGraphicsPixmapItem;
m_pixmapItem->setZValue(-1);
scene()->addItem(m_pixmapItem);
m_rectItem = new QGraphicsRectItem;
m_rectItem->setPen(QPen(Qt::red, 2));
m_rectItem->setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemSendsGeometryChanges);
scene()->addItem(m_rectItem);
}
void ImageView::setImage(const QPixmap& image)
{
m_pixmapItem->setPixmap(image);
setSceneRect(image.rect());
}
void ImageView::setRect(const QRectF& rect)
{
m_rectItem->setRect(rect);
}
void ImageView::mouseMoveEvent(QMouseEvent* event)
{
QPointF pos = mapToScene(event->pos());
QPointF delta = pos - m_lastPos;
m_lastPos = pos;
if (m_rectItem->isSelected()) {
QRectF rect = m_rectItem->rect();
if (m_rectItem->cornerAt(pos) == QGraphicsItem::TopLeftCorner) {
rect.translate(delta);
rect.setWidth(rect.width() - delta.x());
rect.setHeight(rect.height() - delta.y());
} else if (m_rectItem->cornerAt(pos) == QGraphicsItem::TopRightCorner) {
rect.translate(QPointF(0, delta.y()));
rect.setWidth(rect.width() + delta.x());
rect.setHeight(rect.height() - delta.y());
} else if (m_rectItem->cornerAt(pos) == QGraphicsItem::BottomLeftCorner) {
rect.translate(QPointF(delta.x(), 0));
rect.setWidth(rect.width() - delta.x());
rect.setHeight(rect.height() + delta.y());
} else if (m_rectItem->cornerAt(pos) == QGraphicsItem::BottomRightCorner) {
rect.setWidth(rect.width() + delta.x());
rect.setHeight(rect.height() + delta.y());
} else if (m_rectItem->isUnderMouse()) {
rect.translate(delta);
}
m_rectItem->setRect(rect);
} else {
QGraphicsView::mouseMoveEvent(event);
}
}
void ImageView::mousePressEvent(QMouseEvent* event)
{
m_lastPos = mapToScene(event->pos());
QGraphicsView::mousePressEvent(event);
}
void ImageView::mouseReleaseEvent(QMouseEvent* event)
{
QGraphicsView::mouseReleaseEvent(event);
}
void ImageView::wheelEvent(QWheelEvent* event)
{
double factor = std::pow(1.2, event->delta() / 240.0);
scale(factor, factor);
}
void ImageView::drawBackground(QPainter* painter, const QRectF& rect)
{
QGraphicsView::drawBackground(painter, rect);
painter->setPen(Qt::lightGray);
for (int x = int(rect.left()) - (int(rect.left()) % 10); x < rect.right(); x += 10) {
painter->drawLine(x, rect.top(), x, rect.bottom());
}
for (int y = int(rect.top()) - (int(rect.top()) % 10); y < rect.bottom(); y += 10) {
painter->drawLine(rect.left(), y, rect.right(), y);
}
}
```
在主窗口中,我们创建一个ImageView对象,将其添加到布局中,并设置图片和矩形框:
```cpp
MainWindow::MainWindow(QWidget* parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
QPixmap image(":/image.png");
ui->imageView->setImage(image);
ui->imageView->setRect(QRectF(100, 100, 200, 200));
}
```
这样就完成了在QLabel中显示图片并绘制可拖动的矩形框的功能。
阅读全文