qt c++重写继承QGraphicsView在图上显示点并且可以和图像一起进行缩放
时间: 2023-05-16 11:07:25 浏览: 125
可以通过在重写的QGraphicsView中添加QGraphicsItem来显示点,并且可以通过重写QGraphicsView的resizeEvent()函数来实现和图像一起进行缩放。具体实现可以参考以下代码:
```
class MyGraphicsView : public QGraphicsView
{
public:
MyGraphicsView(QWidget *parent = nullptr) : QGraphicsView(parent)
{
QGraphicsScene *scene = new QGraphicsScene(this);
setScene(scene);
}
void addPoint(QPointF point)
{
QGraphicsEllipseItem *item = new QGraphicsEllipseItem(QRectF(point.x()-2, point.y()-2, 4, 4));
item->setPen(QPen(Qt::red));
scene()->addItem(item);
}
protected:
void resizeEvent(QResizeEvent *event) override
{
QGraphicsView::resizeEvent(event);
fitInView(scene()->sceneRect(), Qt::KeepAspectRatio);
}
};
```
在使用时,可以通过调用addPoint()函数来添加点,例如:
```
MyGraphicsView *view = new MyGraphicsView();
view->addPoint(QPointF(100, 100));
```
这样就可以在图上显示点,并且可以和图像一起进行缩放了。
阅读全文
相关推荐
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.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)
![7z](https://img-home.csdnimg.cn/images/20241231044736.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231044955.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![7z](https://img-home.csdnimg.cn/images/20241231044736.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231044955.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231044955.png)