【发布时间】:2021-04-18 10:14:32
【问题描述】:
我正在做一个图像查看器,允许用户浏览他们的文件夹并显示文件夹中的第一张图像。通过滚动鼠标滚轮,用户可以放大和缩小图像。但是,由于鼠标滚轮的滚动功能仍然存在,因此图像也会上下平移。我尝试 scrollbaralwaysoff 但它也不起作用。我可以知道如何在保留缩放功能的同时禁用滚动功能吗?
from PySide2.QtCore import *
from PySide2.QtGui import *
from PySide2.QtWidgets import *
import os
from tg_audit import Ui_MainWindow
class mainProgram (QMainWindow, Ui_MainWindow):
def __init__(self):
super().__init__()
self.setupUi(self)
self.browser()
def browser(self):
self.model = QFileSystemModel(self.centralwidget)
self.model.setRootPath('')
self.treeView.setModel(self.model)
self.treeView.setAnimated(False)
self.treeView.setIndentation(20)
self.treeView.setSortingEnabled(True)
self.treeView.clicked.connect(self.open_image)
def open_image(self, index):
#get path from browser
path = self.sender().model().filePath(index)
print(path)
self.folder_path = r'{}'.format(path)
self.list_of_images = os.listdir(self.folder_path)
self.list_of_images = sorted(self.list_of_images)
#path of the image
input_img_raw_string = r'{}\\{}'.format(path,self.list_of_images[0])
#load image path to graphic view
self.scene = QGraphicsScene()
self.scene.addItem(QGraphicsPixmapItem(QPixmap.fromImage(QImage(input_img_raw_string))))
self.graphicsView.setScene(self.scene)
self.graphicsView.fitInView(self.scene.sceneRect(),Qt.KeepAspectRatio)
self.zoom = 1
self.rotate = 0
def wheelEvent(self, event):
x = event.angleDelta().y() / 120
if x > 0:
self.zoom *= 1.05
self.updateView()
elif x < 0:
self.zoom /= 1.05
self.updateView()
def updateView(self):
self.graphicsView.setTransform(QTransform().scale(self.zoom, self.zoom).rotate(self.rotate))
if __name__ == '__main__':
import sys
from PySide2.QtWidgets import QApplication
app = QApplication(sys.argv)
imageViewer = mainProgram()
imageViewer.show()
sys.exit(app.exec_())
【问题讨论】: