Last active
February 28, 2022 09:19
-
-
Save iTrooz/3a528439096b483a305d0184ce8bcb27 to your computer and use it in GitHub Desktop.
Simple window with mouse scrolling (PyQt5) (based on https://gist.github.com/acbetter/e7d0c600fdc0865f4b0ee05a17b858f2)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
from PyQt5 import QtCore, QtWidgets | |
from PyQt5.QtGui import * | |
from PyQt5.QtWidgets import * | |
from PyQt5.QtCore import * | |
import sys | |
import traceback | |
def exception_hook(exctype, value, a): | |
a = traceback.format_exception(exctype, value, a) | |
print("".join(a)) | |
sys.excepthook = exception_hook | |
class HelloWindow(QMainWindow): | |
def __init__(self): | |
QMainWindow.__init__(self) | |
self.setMinimumSize(QSize(200, 200)) | |
centralWidget = QWidget(self) | |
self.setCentralWidget(centralWidget) | |
gridLayout = QGridLayout(self) | |
centralWidget.setLayout(gridLayout) | |
self.label = QLabel("Hello World from PyQt", self) | |
self.label.setPixmap(QPixmap("test.png")) | |
self.scroller = QScrollArea() | |
self.scroller.setWidget(self.label) | |
self.scroller.setCursor(Qt.CrossCursor) | |
gridLayout.addWidget(self.scroller, 0, 0) | |
self.scroller.mouseMoveEvent = self.mouseMoveEventLeft | |
self.scroller.mousePressEvent = self.mousePressEventLeft | |
self.scroller.mouseReleaseEvent = self.mouseReleaseEventLeft | |
def mousePressEventLeft(self, event): | |
self.label.setCursor(Qt.ClosedHandCursor) | |
self.initialPosX = self.scroller.horizontalScrollBar().value() + event.pos().x() | |
self.initialPosY = self.scroller.verticalScrollBar().value() + event.pos().y() | |
def mouseReleaseEventLeft(self, event): | |
self.label.setCursor(Qt.OpenHandCursor) | |
def mouseMoveEventLeft(self, event): | |
self.scroller.horizontalScrollBar().setValue(self.initialPosX - event.pos().x()) | |
self.scroller.verticalScrollBar().setValue(self.initialPosY - event.pos().y()) | |
app = QtWidgets.QApplication(sys.argv) | |
mainWin = HelloWindow() | |
mainWin.show() | |
sys.exit( app.exec_() ) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment