Skip to content

Instantly share code, notes, and snippets.

@eyllanesc
Created February 27, 2019 18:29
Show Gist options
  • Save eyllanesc/96a51beb47e753f83b8a35bc87e9ece0 to your computer and use it in GitHub Desktop.
Save eyllanesc/96a51beb47e753f83b8a35bc87e9ece0 to your computer and use it in GitHub Desktop.
from PySide2 import QtCore, QtGui, QtWidgets
class InfoBox(QtWidgets.QGraphicsItem):
Type = QtWidgets.QGraphicsItem.UserType + 1
def __init__(self, parent=None):
super(InfoBox, self).__init__(parent)
self.setZValue(4)
proxy = QtWidgets.QGraphicsProxyWidget(self)
widget = QtWidgets.QLabel("TEST!")
widget.setAttribute(QtCore.Qt.WA_TransparentForMouseEvents)
widget.setWindowModality(QtCore.Qt.NonModal)
proxy.setWidget(widget)
def boundingRect(self):
return QtCore.QRectF(0, 0, 100, 100)
def paint(self, *args):
pass
class GraphicsView(QtWidgets.QGraphicsView):
def __init__(self, parent=None):
super(GraphicsView, self).__init__(parent)
self.setScene(QtWidgets.QGraphicsScene(QtCore.QRectF(-400, -400, 800, 800)))
self.setDragMode(QtWidgets.QGraphicsView.ScrollHandDrag)
self.viewport().setCursor(QtCore.Qt.ArrowCursor)
self.create_items()
self.info_box = None
def create_items(self):
rect_item = QtWidgets.QGraphicsRectItem(QtCore.QRect(0, 0, 100, 100))
rect_item.setBrush(QtGui.QColor("#00ff54"))
self.scene().addItem(rect_item)
circle_item = QtWidgets.QGraphicsEllipseItem(QtCore.QRect(-100, -100, 100, 100))
circle_item.setBrush(QtGui.QColor("salmon"))
self.scene().addItem(circle_item)
def mousePressEvent(self, event):
super(GraphicsView, self).mousePressEvent(event)
if self.info_box is None:
self.info_box = InfoBox()
self.scene().addItem(self.info_box)
self.info_box.setPos(self.mapToScene(event.pos()))
def mouseReleaseEvent(self, event):
super(GraphicsView, self).mouseReleaseEvent(event)
self.viewport().setCursor(QtCore.Qt.ArrowCursor)
class MainWindow(QtWidgets.QMainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.setCentralWidget(GraphicsView())
self.resize(640, 480)
if __name__ == '__main__':
import sys
app = QtWidgets.QApplication.instance()
if app is None:
app = QtWidgets.QApplication(sys.argv)
w = MainWindow()
w.show()
sys.exit(app.exec_())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment