Aktuell will ich bei einem Mausklick in die GraphicsView die aktuellen 'absoluten' Koordinaten der Items in der Scene anzeigen. Nun wahrscheinlich suche ich schon wieder zuweit .
Der erste Code habe ich irgendwo gefunden. Der macht mal aus meiner Sicht das was ich möchte. Was ich nicht verstehe, wer ruft die Methode 'def mousePressEvent' auf?
Wie kann ich das jetzt auf mein Code anwenden, da ich meine GUI per QTDesigner erstelle sprich ui-File. Die Idee wäre in der 'Ui_MainWindow' ein entsprechender Slot einzurichten aber da scheint für graphicsView nichts passendes da zu sein.
Code: Alles auswählen
import sys
from PyQt5 import QtWidgets
from PyQt5.QtWidgets import QGraphicsView, QGraphicsScene
class MainWindow(QtWidgets.QMainWindow):
central_widget = None
layout_container = None
def __init__(self):
super(MainWindow, self).__init__()
self.central_widget = QtWidgets.QWidget()
self.layout_container = QtWidgets.QVBoxLayout()
self.central_widget.setLayout(self.layout_container)
self.setCentralWidget(self.central_widget)
self.layout_container.addWidget(MyGraphicsView())
class MyGraphicsView(QGraphicsView):
def __init__(self):
super(MyGraphicsView, self).__init__()
self.setScene(QGraphicsScene())
def mousePressEvent(self, event):
print('view', event.pos())
print('scene', self.mapToScene(event.pos()))
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
mw = MainWindow()
mw.show()
sys.exit(app.exec_())
Code: Alles auswählen
from os import path
import sys
from pathlib import Path
from PyQt5 import QtGui, QtWidgets, QtCore, uic
from PyQt5.QtWidgets import QGraphicsView, QGraphicsScene, QGraphicsItem, QGraphicsPixmapItem, QGraphicsLineItem
MAIN_PATH = Path('g_nav')
IMAGE_PATH = Path('g_nav', 'image')
ORTHO_PATH = Path('g_nav', 'ortho', 'ortho_full')
GUI_NAME = 'g_nav_gui.ui'
class MapDraw():
def __init__(self):
self.scene = QGraphicsScene()
def add_tile(self, pixmap, x, y):
tile = QGraphicsPixmapItem()
tile.setPixmap(pixmap)
tile.moveBy(x, y)
self.scene.addItem(tile)
class Ui_MainWindow(QtWidgets.QMainWindow, QGraphicsView):
def __init__(self, parent=None):
super().__init__(parent)
ui_path = Path(MAIN_PATH, GUI_NAME)
self.ui = uic.loadUi(ui_path, self)
self.ui.graphicsView.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)
self.ui.graphicsView.setHorizontalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)
self.ui.graphicsView.setDragMode(QtWidgets.QGraphicsView.ScrollHandDrag)
self.geo_map = MapDraw()
self.ui.graphicsView.setScene(self.geo_map.scene)
self.pixmap = QtGui.QPixmap(str(Path(ORTHO_PATH, 'tile27.png')))
self.geo_map.add_tile(self.pixmap, 0, 0)
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
MainWindow = Ui_MainWindow()
MainWindow.show()
sys.exit(app.exec_())