[PyQt] why is mouseMoveEvent not reached in QMainWindow object

Brian Kelley kelley at eyesopen.com
Thu Apr 29 14:18:50 BST 2010


You need to have a:

def mouseMoveEvent(...)

for each widget, i.e. the mainwindow.mouseMoveEvent doesn't see mouse events from the GraphicsView since these are accepted by the graphics view and not propagated.

Briefly looking at your code, I would move the mouseMove function to the GraphicsView (you might need to send in the coordinate label to the constructor).

Brian

On Apr 29, 2010, at 8:38 AM, <christopher.nahler at papermodels.at> <christopher.nahler at papermodels.at> wrote:

> 
> I have expanded my working code where I replace the central QLabel widget
> with a scene and view. Now the mouse tracking works when the cursor is over
> the status bar and the label in the statusbar but not over the graphics
> view. But when I press the mouse button and move out of the statusbar area
> into the graphics view area the tracking works. Just not when I move the
> mouse without clicking over the view. I am guessing this has to do with the
> rubberbanding? Or have I missed something else?
> 
> The view has setMouseTracking set to True and the scene does not have this
> function. 
> 
> from PyQt4.QtCore import *
> from PyQt4.QtGui import *
> 
> SCREEN_BORDER = 100
> 
> class GraphicsView(QGraphicsView):
>    def __init__(self, parent=None):
>        super(GraphicsView, self).__init__(parent)
>        self.setMouseTracking(True)
>        self.setDragMode(QGraphicsView.RubberBandDrag)
> 
>    #def wheelEvent(self, event):
>        #factor = 1.41 ** (-event.delta() / 240.0)
>        #self.scale(factor, factor)
> 
> 
> class MouseCoordinates(QLabel):
> 
>    def __init__(self, parent=None):
>        super(MouseCoordinates, self).__init__(parent)
>        self.setMouseTracking(True)
>        self.curPos = None
>        self.setText(QString())
>        self.update()
> 
>    def paintEvent(self, event):
>        painter = QPainter(self)
>        self.curPos = QCursor.pos()
>        self.setText(" mouse @ %d / %d " % (self.curPos.x(),
> self.curPos.y()))
>        painter.drawText(self.rect(), Qt.AlignCenter, self.text())
> 
> 
> class MainWindow(QMainWindow):
> 
>    def __init__(self, parent=None):
>        super(MainWindow, self).__init__(parent)
>        self.setMouseTracking(True)
> 
>        self.scene = QGraphicsScene(self)
>        self.scene.setSceneRect(QRectF(0, 0, 800, 600))
> 
>        # draw border
>        self.scene.addRect(QRectF(0, 0, 800, 600), 
>                           QPen(Qt.darkGray, 1, Qt.DotLine),
>                           QBrush(Qt.lightGray))
> 
>        # save the current rect as parent object (canvas) for drawing
>        self.background = self.scene.items()[0]
> 
>        myObj = QGraphicsRectItem(QRectF(100, 100, 200, 200), parent =
> self.background, scene = self.scene)
>        myPen = QPen(Qt.black, 1, Qt.SolidLine)
>        myBrush = QBrush(Qt.white, Qt.SolidPattern)
>        myObj.setFlag(QGraphicsItem.ItemIsMovable, True)
>        myObj.setFlag(QGraphicsItem.ItemIsSelectable, True)
>        myObj.setPen(myPen)
>        myObj.setBrush(myBrush)
> 
>        # add view
>        self.view = GraphicsView()
>        # self.view.setMouseTracking(True) # set in class already
>        self.view.setScene(self.scene)
>        self.setCentralWidget(self.view)
> 
>        self.coordsLabel = MouseCoordinates()
>        #self.coordsLabel.setFrameStyle(QFrame.StyledPanel|QFrame.Sunken)
>        status = self.statusBar()
>        status.setMouseTracking(True)
>        status.setSizeGripEnabled(False)
>        status.addPermanentWidget(self.coordsLabel)
>        status.showMessage("Ready", 5000)
> 
> 
>    def mouseMoveEvent(self, event):
>        self.coordsLabel.update()
>        super(MainWindow, self).mouseMoveEvent(event)         
> 
> if __name__ == "__main__":
> 
>    import sys
> 
>    # setup application object
>    app = QApplication(sys.argv)
> 
>    # create (parent) main window
>    mainWindow = MainWindow()
>    rect = QApplication.desktop().availableGeometry()
>    mainWindow.setGeometry(rect.x() + SCREEN_BORDER, 
>                           rect.y() + SCREEN_BORDER, 
>                           rect.width() - 2 * SCREEN_BORDER, 
>                           rect.height() - 2 * SCREEN_BORDER)
>    mainWindow.setMinimumSize(900, 700)
> 
> mainWindow.setWindowIcon(QIcon("D:\UDaten\pyqt\chap12\DesignerTest.bmp"))
>    mainWindow.setWindowTitle("DesignerTest")
>    mainWindow.show()
> 
>    # run application object
>    sys.exit(app.exec_())



More information about the PyQt mailing list