qt 重写继承QGraphicsView,打开图像显示时自适应,以图像大小为坐标并显示鼠标移动和停留时的的坐标,CTRL和鼠标左键按下可移动图像,鼠标右键出现菜单栏,鼠标滚轮可放大缩小
时间: 2023-05-17 19:02:36 浏览: 439
您可以使用以下代码来实现这个功能:
```
class ImageView : public QGraphicsView
{
public:
ImageView(QWidget *parent = nullptr) : QGraphicsView(parent)
{
setDragMode(QGraphicsView::ScrollHandDrag);
setRenderHint(QPainter::Antialiasing);
setRenderHint(QPainter::SmoothPixmapTransform);
setOptimizationFlag(QGraphicsView::DontAdjustForAntialiasing, true);
setViewportUpdateMode(QGraphicsView::FullViewportUpdate);
setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
setAlignment(Qt::AlignCenter);
setInteractive(true);
setMouseTracking(true);
setContextMenuPolicy(Qt::CustomContextMenu);
setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
}
void setImage(const QImage &image)
{
QGraphicsScene *scene = new QGraphicsScene(this);
QGraphicsPixmapItem *item = new QGraphicsPixmapItem(QPixmap::fromImage(image));
scene->addItem(item);
setScene(scene);
fitInView(item, Qt::KeepAspectRatio);
}
protected:
void mousePressEvent(QMouseEvent *event) override
{
if (event->modifiers() == Qt::ControlModifier && event->button() == Qt::LeftButton)
{
setDragMode(QGraphicsView::ScrollHandDrag);
lastPos = event->pos();
}
else if (event->button() == Qt::RightButton)
{
QMenu menu(this);
QAction *zoomInAction = menu.addAction(tr("Zoom In"));
QAction *zoomOutAction = menu.addAction(tr("Zoom Out"));
QAction *resetAction = menu.addAction(tr("Reset"));
QAction *selectedAction = menu.exec(event->globalPos());
if (selectedAction == zoomInAction)
{
scale(1.2, 1.2);
}
else if (selectedAction == zoomOutAction)
{
scale(1 / 1.2, 1 / 1.2);
}
else if (selectedAction == resetAction)
{
fitInView(scene()->itemsBoundingRect(), Qt::KeepAspectRatio);
}
}
QGraphicsView::mousePressEvent(event);
}
void mouseMoveEvent(QMouseEvent *event) override
{
QPointF scenePos = mapToScene(event->pos());
emit mouseMoved(scenePos.toPoint());
QGraphicsView::mouseMoveEvent(event);
}
void mouseReleaseEvent(QMouseEvent *event) override
{
if (event->button() == Qt::LeftButton)
{
setDragMode(QGraphicsView::NoDrag);
}
QGraphicsView::mouseReleaseEvent(event);
}
void wheelEvent(QWheelEvent *event) override
{
if (event->modifiers() == Qt::ControlModifier)
{
if (event->delta() > 0)
{
scale(1.2, 1.2);
}
else if (event->delta() < 0)
{
scale(1 / 1.2, 1 / 1.2);
}
}
else
{
QGraphicsView::wheelEvent(event);
}
}
void resizeEvent(QResizeEvent *event) override
{
fitInView(scene()->itemsBoundingRect(), Qt::KeepAspectRatio);
QGraphicsView::resizeEvent(event);
}
signals:
void mouseMoved(const QPoint &pos);
private:
QPoint lastPos;
};
```
您可以使用以下代码来测试:
```
QImage image(":/path/to/image.png");
ImageView *view = new ImageView;
view->setImage(image);
view->show();
```
请注意,这只是一个简单的示例,您可能需要根据您的需求进行修改。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)