Skip to content

Instantly share code, notes, and snippets.

@eyllanesc
Created July 19, 2017 12:59
Show Gist options
  • Save eyllanesc/3db9dc48904dc86921f9fa4215665bd0 to your computer and use it in GitHub Desktop.
Save eyllanesc/3db9dc48904dc86921f9fa4215665bd0 to your computer and use it in GitHub Desktop.
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *
import sys
class Stadium(QWidget):
def __init__(self, pixmap, parent=None):
QWidget.__init__(self, parent=parent)
self.pixmap = pixmap
self.pos = None
self.setMouseTracking(True)
def paintEvent(self, event):
painter = QPainter(self)
w = min(1.0*self.width()/self.pixmap.width(), 1.0*self.height()/self.pixmap.height())
size = w*self.pixmap.size()
painter.drawPixmap(QRect(QPoint(), size), self.pixmap.scaled(size, Qt.KeepAspectRatio))
painter.setPen(QPen(Qt.red, 15, Qt.SolidLine))
if self.pos:
painter.drawEllipse(self.pos, 15, 15)
def mouseMoveEvent(self, event):
self.pos = event.pos()
self.update()
class Widget(QWidget):
def __init__(self, parent=None):
QWidget.__init__(self, parent=parent)
self.setLayout(QVBoxLayout())
label = Stadium(QPixmap("image.png"))
self.layout().addWidget(label)
self.resize(640, 480)
if __name__ == '__main__':
app = QApplication(sys.argv)
w = Widget()
w.show()
sys.exit(app.exec_())
class Stadium(QWidget):
def __init__(self, pixmap, parent=None):
QWidget.__init__(self, parent=parent)
self.pixmap = pixmap
self.pos = None
self.setMouseTracking(True)
def paintEvent(self, event):
painter = QPainter(self)
w = min(1.0*self.width()/self.pixmap.width(), 1.0*self.height()/self.pixmap.height())
size = w*self.pixmap.size()
painter.drawPixmap(QRect(QPoint(), size), self.pixmap.scaled(size, Qt.KeepAspectRatio))
painter.setPen(QPen(Qt.red, 15, Qt.SolidLine))
if self.pos:
painter.drawEllipse(self.pos, 15, 15)
def mouseMoveEvent(self, event):
self.pos = event.pos()
self.update()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment