I am doing an Image Viewer that allow user to browse their folder and show the first image in the folder. By scrolling the mouse wheel, user is able to zoom in and out the image. However, the image pan up and down too as the scroll feature of the mouse wheel is still there. I try scrollbaralwaysoff but it doestn work too. May I know how to disable the scroll feature while remaining my zooming feature?
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_())