from PyQt6.QtWidgets import QGraphicsView, QGraphicsScene, QMainWindow, QApplication
from PyQt6.QtCore import Qt
from PyQt6.QtGui import QPixmap, QMouseEvent
from PyQt6 import uic
class CustomGraphicsView(QGraphicsView):
def __init__(self, parent=None):
super(CustomGraphicsView, self).__init__(parent)
self.scene = QGraphicsScene()
self.setScene(self.scene)
def load_image(self, image_path):
pixmap = QPixmap(image_path)
self.scene.clear()
self.scene.addPixmap(pixmap)
self.fitInView(self.scene.itemsBoundingRect(), 1)
def change_image(self, new_image_path):
self.load_image(new_image_path)
def mousePressEvent(self, event: QMouseEvent):
if event.button() == Qt.MouseButton.LeftButton:
print("Presionaste el botón izquierdo del mouse.")
super(CustomGraphicsView, self).mousePressEvent(event)
def mouseMoveEvent(self, event: QMouseEvent):
if event.buttons() == Qt.MouseButton.LeftButton:
print("Estás arrastrando el puntero con el botón izquierdo presionado.")
super(CustomGraphicsView, self).mouseMoveEvent(event)
def mouseReleaseEvent(self, event: QMouseEvent):
if event.button() == Qt.MouseButton.LeftButton:
print("Soltaste el botón izquierdo del mouse.")
super(CustomGraphicsView, self).mouseReleaseEvent(event)
class Main(QMainWindow):
def __init__(self):
super(Main, self).__init__()
uic.loadUi('tu_archivo.ui', self)
# Encuentra el QGraphicsView en la interfaz cargada
graphicsView = self.findChild(QGraphicsView, 'graphicsView')
# Reemplaza el QGraphicsView con tu CustomGraphicsView
self.customGraphicsView = CustomGraphicsView(self)
self.customGraphicsView.setGeometry(graphicsView.geometry())
graphicsView.setParent(None) # Desconecta el QGraphicsView original
self.setCentralWidget(self.customGraphicsView) # Establece el CustomGraphicsView como el widget central
self.pushButton.clicked.connect(self.on_pushButton_clicked)
def on_pushButton_clicked(self):
image_path = 'ruta/a/la/imagen.png'
self.customGraphicsView.load_image(image_path)
def cambiar_imagen(self, nueva_imagen):
self.customGraphicsView.change_image(nueva_imagen)
if __name__ == "__main__":
import sys
app = QApplication(sys.argv)
window = Main()
window.show()
sys.exit(app.exec())
To embed this project on your website, copy the following code and paste it into your website's HTML: