PYOT5QGraphicsView无法自适应图片大小问题
时间: 2023-09-12 18:05:54 浏览: 182
如果您希望PYOT5的QGraphicsView控件能够自适应图片大小,可以使用以下方法:
1. 在设置图片之前,先获取QGraphicsView控件的大小。
```python
width = self.graphicsView.width()
height = self.graphicsView.height()
```
2. 设置图片,并获取图片的大小。
```python
pixmap = QtGui.QPixmap("image.png")
imageWidth = pixmap.width()
imageHeight = pixmap.height()
```
3. 计算缩放比例。
```python
scaleWidth = width / imageWidth
scaleHeight = height / imageHeight
scale = min(scaleWidth, scaleHeight)
```
4. 缩放图片,并设置到QGraphicsView控件中。
```python
pixmap = pixmap.scaled(imageWidth * scale, imageHeight * scale, QtCore.Qt.KeepAspectRatio)
self.graphicsView.setScene(QtGui.QGraphicsScene())
self.graphicsView.scene().addPixmap(pixmap)
```
这样设置后,QGraphicsView控件就会自适应图片大小了。
相关问题
python pyOt
您好!pyOt 是一个 Python 库,用于将 OpenType 字体文件转换为 Python 对象。它提供了一种方便的方式来处理字体文件,包括读取和修改字体属性、检查字形和轮廓数据等。您可以使用 pyOt 来扩展和定制字体的功能。如果您有任何关于 pyOt 的具体问题或者其他问题,我会很乐意帮助您!
阅读全文
相关推荐












