qt c++重写继承QGraphicsView.打开图像显示时自适应
时间: 2023-05-17 12:02:48 浏览: 244
您可以通过重写QGraphicsView的resizeEvent()函数来实现自适应显示图像。在该函数中,您可以获取QGraphicsView的大小并将其设置为场景的大小,然后调用fitInView()函数以确保图像适合视图。
以下是示例代码:
```
class MyGraphicsView : public QGraphicsView {
public:
MyGraphicsView(QWidget *parent = nullptr) : QGraphicsView(parent) {}
protected:
void resizeEvent(QResizeEvent *event) override {
QGraphicsView::resizeEvent(event);
fitInView(scene()->sceneRect(), Qt::KeepAspectRatio);
}
};
```
您可以将此类用作QGraphicsView的子类,并在需要显示图像时使用它来替换默认的QGraphicsView。
阅读全文
相关推荐
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231044955.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![7z](https://img-home.csdnimg.cn/images/20241231044736.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![-](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/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)