[PyQt] why is mouseMoveEvent not reached in QMainWindow object
Brian Kelley
kelley at eyesopen.com
Wed Apr 28 16:58:03 BST 2010
You need to turn mouse tracking on, it is off by default:
http://doc.trolltech.com/4.6/qwidget.html#mouseTracking-prop
Brian Kelley
On Apr 28, 2010, at 9:37 AM, Christopher M. Nahler wrote:
> I am new to PyQt4 and trying to find my way ... here n the first example
> code I am successfully trying to display the mouse coordinates in a
> little app using a QWidget object as main window. Below is another try
> where I would like to display the mouse coordinates in the status bar of
> a QMainWindow object. Somehow I cannot reach the mouseMoveEvent
> function. What am I doing wrong?
>
> import sys
> from PyQt4.QtCore import *
> from PyQt4.QtGui import *
>
> class MainWindow(QWidget):
>
> def __init__(self, parent=None):
> super(MainWindow, self).__init__(parent)
>
> self.setMouseTracking(True)
> self.text = QString()
>
> self.resize(400, 200)
> self.move(100, 100)
> self.setWindowTitle("MouseMoveTest")
> self.update()
>
> def paintEvent(self, event):
> painter = QPainter(self)
> painter.drawText(self.rect(), Qt.AlignCenter, self.text)
>
> def mouseMoveEvent(self, event):
> curPos = QCursor.pos()
> self.text = " mouse @ %d / %d " % (curPos.x(), curPos.y())
> self.update()
>
> app = QApplication(sys.argv)
> form = MainWindow()
> form.show()
> app.exec_()
>
> --------------------------------------
>
> import sys
> from PyQt4.QtCore import *
> from PyQt4.QtGui import *
>
> SCREEN_BORDER = 100
>
> class myLabel(QLabel):
> def __init__(self, parent=None):
> super(myLabel, self).__init__(parent)
> curPos = QCursor.pos()
> self.setText(" mouse @ %d / %d " % (curPos.x(), curPos.y()))
> self.update()
>
> def paintEvent(self, event):
> painter = QPainter(self)
> painter.drawText(self.rect(), Qt.AlignCenter, self.text())
>
>
> class MainWindow(QMainWindow):
>
> def __init__(self, parent=None):
> super(MainWindow, self).__init__(parent)
>
> self.imageLabel = QLabel()
> self.setCentralWidget(self.imageLabel)
>
> # statusbar
> status = self.statusBar()
> status.setSizeGripEnabled(False)
>
> # add label for mouse coords in statusbar
> self.coordsLabel = myLabel()
> status.addPermanentWidget(self.coordsLabel)
> status.showMessage("Ready", 5000)
>
>
> def mouseMoveEvent(self, event):
> curPos = QCursor.pos()
> print curPos
> self.coordsLabel.setText(" mouse @ %d / %d " % (curPos.x(),
> curPos.y()))
> 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_())
> _______________________________________________
> PyQt mailing list PyQt at riverbankcomputing.com
> http://www.riverbankcomputing.com/mailman/listinfo/pyqt
More information about the PyQt
mailing list