Created
July 19, 2017 12:59
-
-
Save eyllanesc/3db9dc48904dc86921f9fa4215665bd0 to your computer and use it in GitHub Desktop.
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
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_()) |
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
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