PyQT4: mousePressEvent bei QGraphicsScene

Python und das Qt-Toolkit, erstellen von GUIs mittels des Qt-Designers.
Antworten
nicon
User
Beiträge: 10
Registriert: Freitag 21. September 2007, 18:09

Montag 1. Oktober 2007, 09:08

Hallo Leute,

vielleicht kann mir jemand helfen.

Ich möchte in meinem Programm gerne ein QRubberBand einbinden, mausgesteuert. Das RubberBand soll die gesetzten Items markieren. Dazu muss ich die MouseEvents wie z.B. mousePressEvent
abfangen können. Leider bekomme ich das nicht hin.

Kann mir jemand einen Tipp geben?

Hier mein Code:

Code: Alles auswählen

import sys
from PyQt4 import QtCore, QtGui

from PyQt4.QtCore import *
from PyQt4.QtGui import *

class NETVISO_add_rect(QGraphicsRectItem):
    def __init__(self):
        QGraphicsRectItem.__init__(self, QRectF(2500, 2500, 40, 40))
        self.setFlag(QGraphicsItem.ItemIsMovable)
	self.setFlag(QGraphicsItem.ItemIsSelectable)
	self.setFlag(QGraphicsItem.ItemIsFocusable)
        self.setAcceptsHoverEvents(True)
		
	  
class NETVISO_add_item(QGraphicsPixmapItem):
    def __init__(self,MAP_ITEM):
        
	if MAP_ITEM == "computer":
	    QGraphicsPixmapItem.__init__(self, QPixmap("./images/icons/map_computer.png"))
	    self.setFlag(QGraphicsItem.ItemIsMovable)
	    self.setFlag(QGraphicsItem.ItemIsSelectable)
	    self.setFlag(QGraphicsItem.ItemIsFocusable)
	    self.setAcceptsHoverEvents(True)
	    
	
	elif MAP_ITEM == "pda":
	    QGraphicsPixmapItem.__init__(self, QPixmap("./images/icons/map_pda.png"))
            self.setFlag(QGraphicsItem.ItemIsMovable)
	    self.setFlag(QGraphicsItem.ItemIsSelectable)
	    self.setFlag(QGraphicsItem.ItemIsFocusable)
	    self.setAcceptsHoverEvents(True)
	
	elif MAP_ITEM == "printer":
	    QGraphicsPixmapItem.__init__(self, QPixmap("./images/icons/map_printer.png"))
            self.setFlag(QGraphicsItem.ItemIsMovable)
	    self.setFlag(QGraphicsItem.ItemIsSelectable)
	    self.setFlag(QGraphicsItem.ItemIsFocusable)
	    self.setAcceptsHoverEvents(True)
	    
    
    
    def mousePressEvent(self, event):
        self.update()
	print "press"
        QtGui.QGraphicsItem.mousePressEvent(self, event)

    def mouseReleaseEvent(self, event):
        self.update()
	#self.setFocus()
	print "release"
        QtGui.QGraphicsItem.mouseReleaseEvent(self, event)
	
    def hoverEnterEvent(self, event):
        self.update()
	print "hoverEnter"
	
    def hoverLeaveEvent(self, event):
        self.update()
	print "hoverLeave"
       




class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(QtCore.QSize(QtCore.QRect(0,0,800,600).size()).expandedTo(MainWindow.minimumSizeHint()))


        self.scene = QtGui.QGraphicsScene()
        self.scene.setSceneRect(0, 0, 5000, 5000)
	self.scene.setSelectionArea(QPainterPath())
        self.scene.setItemIndexMethod(QtGui.QGraphicsScene.NoIndex)
			
	self.box = NETVISO_add_rect()
        self.scene.addItem(self.box)
	
	

        self.graphicsView = QtGui.QGraphicsView(self.scene)
        self.graphicsView.setGeometry(QtCore.QRect(10,10,711,421))
        self.graphicsView.setRenderHint(QtGui.QPainter.Antialiasing)
	self.graphicsView.setBackgroundBrush(QtGui.QColor(217, 225, 232))
        self.graphicsView.setCacheMode(QtGui.QGraphicsView.CacheBackground)
        #self.graphicsView.setDragMode(QtGui.QGraphicsView.ScrollHandDrag)
	self.graphicsView.setAcceptDrops(True)
        self.graphicsView.setWindowTitle(QtCore.QT_TRANSLATE_NOOP(QtGui.QGraphicsView, "NVISO_MAP"))
	MainWindow.setCentralWidget(self.graphicsView)

       	
	# QGraphicsItem

	
	

        self.statusbar = QtGui.QStatusBar(MainWindow)
        self.statusbar.setObjectName("statusbar")
        MainWindow.setStatusBar(self.statusbar)

        self.toolBar = QtGui.QToolBar(MainWindow)
        self.toolBar.setAllowedAreas(QtCore.Qt.TopToolBarArea)
        self.toolBar.setObjectName("toolBar")
        MainWindow.addToolBar(QtCore.Qt.TopToolBarArea,self.toolBar)
	
	self.toolBar2 = QtGui.QToolBar(MainWindow)
        self.toolBar2.setAllowedAreas(QtCore.Qt.LeftToolBarArea)
        self.toolBar2.setObjectName("toolBar2")
        MainWindow.addToolBar(QtCore.Qt.LeftToolBarArea,self.toolBar2)



	# Programm-Menü-Leiste #
        self.menuBar = QtGui.QMenuBar(MainWindow)
        self.menuBar.setGeometry(QtCore.QRect(0,0,800,29))
        self.menuBar.setObjectName("menuBar")

        self.menuKarte = QtGui.QMenu(self.menuBar)
        self.menuKarte.setObjectName("menuKarte")
	
	self.menuBearbeiten = QtGui.QMenu(self.menuBar)
        self.menuBearbeiten.setObjectName("menuBearbeiten")
	
	self.menuHilfe = QtGui.QMenu(self.menuBar)
        self.menuHilfe.setObjectName("menuHilfe")
        MainWindow.setMenuBar(self.menuBar)
	
        
        # Programm-Menü-Aktionen #
        self.action_ffnen = QtGui.QAction(MainWindow)
        self.action_ffnen.setObjectName("action_ffnen")

        self.actionSpeichern = QtGui.QAction(MainWindow)
        self.actionSpeichern.setObjectName("actionSpeichern")

        self.actionSpeichern_unter = QtGui.QAction(MainWindow)
        self.actionSpeichern_unter.setObjectName("actionSpeichern_unter")

        self.actionBeenden = QtGui.QAction(MainWindow)
        self.actionBeenden.setObjectName("actionBeenden")
	
	self.actionAlles_markieren = QtGui.QAction(MainWindow)
        self.actionAlles_markieren.setObjectName("actionAlles_markieren")
	
	self.actionAnleitung = QtGui.QAction(MainWindow)
        self.actionAnleitung.setObjectName("actionAnleitung")
	self.actionVersion = QtGui.QAction(MainWindow)
        self.actionVersion.setObjectName("actionVersion")
	
	# Programm-Menü-Einträge #
	self.menuKarte.addAction(self.action_ffnen)
        self.menuKarte.addAction(self.actionSpeichern)
        self.menuKarte.addAction(self.actionSpeichern_unter)
        self.menuKarte.addSeparator()
        self.menuKarte.addAction(self.actionBeenden)
        self.menuBar.addAction(self.menuKarte.menuAction())
	
	self.menuBearbeiten.addAction(self.actionAlles_markieren)
	self.menuBar.addAction(self.menuBearbeiten.menuAction())
	
	self.menuHilfe.addAction(self.actionAnleitung)
	self.menuHilfe.addAction(self.actionVersion)
	self.menuBar.addAction(self.menuHilfe.menuAction())

	
	
	
	
	# Toolbar TOP #
	self.actionNv_open = QtGui.QAction(MainWindow)
        self.actionNv_open.setIcon(QtGui.QIcon("./images/icons/open.png"))
        self.actionNv_open.setObjectName("actionNv_open")
        self.toolBar.addAction(self.actionNv_open)
	self.actionNv_save = QtGui.QAction(MainWindow)
        self.actionNv_save.setIcon(QtGui.QIcon("./images/icons/save.png"))
        self.actionNv_save.setObjectName("actionNv_save")
        self.toolBar.addAction(self.actionNv_save)
        self.actionNv_delete = QtGui.QAction(MainWindow)
        self.actionNv_delete.setIcon(QtGui.QIcon("./images/icons/delete.png"))
        self.actionNv_delete.setObjectName("actionNv_delete")
        self.toolBar.addAction(self.actionNv_delete)
	self.actionNv_print = QtGui.QAction(MainWindow)
        self.actionNv_print.setIcon(QtGui.QIcon("./images/icons/print.png"))
        self.actionNv_print.setObjectName("actionNv_print")
        self.toolBar.addAction(self.actionNv_print)
	
	# Toolbar Left #
	self.actionNv_map_computer = QtGui.QAction(MainWindow)
        self.actionNv_map_computer.setIcon(QtGui.QIcon("./images/icons/computer.png"))
        self.actionNv_map_computer.setObjectName("actionNv_map_computer")
	self.toolBar2.addAction(self.actionNv_map_computer)
	self.actionNv_map_pda = QtGui.QAction(MainWindow)
        self.actionNv_map_pda.setIcon(QtGui.QIcon("./images/icons/pda.png"))
        self.actionNv_map_pda.setObjectName("actionNv_map_computer")
	self.toolBar2.addAction(self.actionNv_map_pda)
	self.actionNv_map_printer = QtGui.QAction(MainWindow)
        self.actionNv_map_printer.setIcon(QtGui.QIcon("./images/icons/printer.png"))
        self.actionNv_map_printer.setObjectName("actionNv_map_computer")
	self.toolBar2.addAction(self.actionNv_map_printer)
	
        
        # Übersetzungen #
        self.retranslateUi(MainWindow)
	
	# Slot-Definitionen #
	QtCore.QObject.connect(self.actionNv_map_computer,QtCore.SIGNAL("activated()"),self.SLOT_addMAPitem_computer)
	QtCore.QObject.connect(self.actionNv_map_pda,QtCore.SIGNAL("activated()"),self.SLOT_addMAPitem_pda)
	QtCore.QObject.connect(self.actionNv_map_printer,QtCore.SIGNAL("activated()"),self.SLOT_addMAPitem_printer)
	
	QtCore.QObject.connect(self.actionAlles_markieren,QtCore.SIGNAL("activated()"),self.SLOT_Alles_markieren)
	QtCore.QMetaObject.connectSlotsByName(MainWindow)

	
    def retranslateUi(self, MainWindow):
        MainWindow.setWindowTitle(QtGui.QApplication.translate("MainWindow", "NetViso 1.0", None, QtGui.QApplication.UnicodeUTF8))
        self.toolBar.setWindowTitle(QtGui.QApplication.translate("MainWindow", "Programmleiste", None, QtGui.QApplication.UnicodeUTF8))
	self.toolBar2.setWindowTitle(QtGui.QApplication.translate("MainWindow", "Werkzeugleiste", None, QtGui.QApplication.UnicodeUTF8))
        self.menuKarte.setTitle(QtGui.QApplication.translate("MainWindow", "Karte", None, QtGui.QApplication.UnicodeUTF8))
	self.menuBearbeiten.setTitle(QtGui.QApplication.translate("MainWindow", "Bearbeiten", None, QtGui.QApplication.UnicodeUTF8))
	self.menuHilfe.setTitle(QtGui.QApplication.translate("MainWindow", "Hilfe", None, QtGui.QApplication.UnicodeUTF8))
        self.action_ffnen.setText(QtGui.QApplication.translate("MainWindow", "Öffnen", None, QtGui.QApplication.UnicodeUTF8))
        self.actionSpeichern.setText(QtGui.QApplication.translate("MainWindow", "Speichern", None, QtGui.QApplication.UnicodeUTF8))
        self.actionSpeichern_unter.setText(QtGui.QApplication.translate("MainWindow", "Speichern unter", None, QtGui.QApplication.UnicodeUTF8))
        self.actionBeenden.setText(QtGui.QApplication.translate("MainWindow", "Beenden", None, QtGui.QApplication.UnicodeUTF8))
	self.actionAlles_markieren.setText(QtGui.QApplication.translate("MainWindow", "Alles markieren", None, QtGui.QApplication.UnicodeUTF8))
	self.actionAnleitung.setText(QtGui.QApplication.translate("MainWindow", "NetViso Anleitung", None, QtGui.QApplication.UnicodeUTF8))
	self.actionVersion.setText(QtGui.QApplication.translate("MainWindow", "Version", None, QtGui.QApplication.UnicodeUTF8))
        self.actionNv_open.setText(QtGui.QApplication.translate("MainWindow", "Karte öffnen", None, QtGui.QApplication.UnicodeUTF8))
	self.actionNv_print.setText(QtGui.QApplication.translate("MainWindow", "Bildschirm drucken", None, QtGui.QApplication.UnicodeUTF8))
	self.actionNv_delete.setText(QtGui.QApplication.translate("MainWindow", "Auswahl löschen", None, QtGui.QApplication.UnicodeUTF8))
	self.actionNv_save.setText(QtGui.QApplication.translate("MainWindow", "Karte speichern", None, QtGui.QApplication.UnicodeUTF8))
	self.actionNv_map_computer.setText(QtGui.QApplication.translate("MainWindow", "Computer einfügen", None, QtGui.QApplication.UnicodeUTF8))
	self.actionNv_map_pda.setText(QtGui.QApplication.translate("MainWindow", "PDA einfügen", None, QtGui.QApplication.UnicodeUTF8))
	self.actionNv_map_printer.setText(QtGui.QApplication.translate("MainWindow", "Drucker einfügen", None, QtGui.QApplication.UnicodeUTF8))




    # Slots und Funktionen #

    def SLOT_addMAPitem_computer(self):
	self.newNETVISIOitem = NETVISO_add_item("computer")
        self.scene.addItem(self.newNETVISIOitem)
	
    def SLOT_addMAPitem_pda(self):
	self.newNETVISIOitem = NETVISO_add_item("pda")
        self.scene.addItem(self.newNETVISIOitem)
	
    def SLOT_addMAPitem_printer(self):
	self.newNETVISIOitem = NETVISO_add_item("printer")
        self.scene.addItem(self.newNETVISIOitem)
	
    def SLOT_Alles_markieren(self):
	print "slotty"
Danke für Eure Hilfe!!
nicon
User
Beiträge: 10
Registriert: Freitag 21. September 2007, 18:09

Montag 1. Oktober 2007, 11:20

Hier ist ein Beispiel, wo es einwandfrei funktioniert.

Leider bekomme ich das Beispiel nicht auf mein Programm übertragen.

Code: Alles auswählen

#!/usr/bin/env python
# -*- encoding: latin1 -*-

import sys
from PyQt4 import QtCore, QtGui



class MainWidget(QtGui.QMainWindow):
	
	def __init__(self, parent=None):
		QtGui.QWidget.__init__(self, parent)
		self.rubberHand = QtGui.QRubberBand(QtGui.QRubberBand.Rectangle, self)
		self.mousePressed = bool(0)
		self.originPressed = QtCore.QPoint(0,0)
		
	def mousePressEvent(self, event):		
		self.mousePressed = bool(1)
		self.originPressed = QtCore.QPoint(event.pos())
		
		#self.rubberHand = QtGui.QRubberBand(QtGui.QRubberBand.Rectangle, self)
		self.rubberHand.setGeometry(QtCore.QRect(self.originPressed, QtCore.QSize()))
		self.rubberHand.show()
		
	def mouseReleaseEvent(self, event):
		self.mousePressed = bool(0)
		self.rubberHand.hide()

	def mouseMoveEvent(self, event):		
		if self.mousePressed:			
			self.rubberHand.setGeometry(QtCore.QRect(self.originPressed, event.pos()).normalized())
		
		

app = QtGui.QApplication(sys.argv)
widget = MainWidget()
widget.show()
sys.exit(app.exec_())
nicon
User
Beiträge: 10
Registriert: Freitag 21. September 2007, 18:09

Montag 1. Oktober 2007, 15:41

Tja, habe die Lösung endlich gefunden.

Code: Alles auswählen

self.graphicsView.setDragMode(QGraphicsView.RubberBandDrag)
So einfach ist das!!!
Antworten