mage
mage

Reputation: 636

PyQt4: QPixmap signals?

Where can I find a list of the signals emitted by QPixmap, with descriptions? I looked in the official docs and I also googled "pyqt4 qpixmap signals" but I couldn't find anything.

I need this because I need to change the image displayed when the mouse hovers over the QPixmap. I was hoping to simply connect a function to a "hover" or "mouseover" signal, but I can't find such a thing.

A more general question: In the future, if I want to find a list of the signals emitted by a certain class, where can I find this information?

Upvotes: 1

Views: 532

Answers (1)

ekhumoro
ekhumoro

Reputation: 120588

The QPixmap class does not define any signals. Only subclasses of QObject can define signals, and QPixmap is not a subclass of QObject. The Qt documentation provides a section listing the signals for every class that defines them (e.g. QWidget).

To track mouse movements, you must monitor the relevant system events which are sent to the widget used to display the pixmap. One way to do this is to install an event-filter on the display widget, and handle its enter and leave events.

Here's a simple demo that shows how to do that:

from PyQt4 import QtCore, QtGui

class Window(QtGui.QWidget):
    def __init__(self):
        super(Window, self).__init__()
        self.pixmap1 = QtGui.QPixmap('image1.jpg')
        self.pixmap2 = QtGui.QPixmap('image2.jpg')
        self.label = QtGui.QLabel(self)
        self.label.setPixmap(self.pixmap1)
        self.label.installEventFilter(self)
        layout = QtGui.QVBoxLayout(self)
        layout.addWidget(self.label)

    def eventFilter(self, widget, event):
        if widget is self.label:
            if event.type() == QtCore.QEvent.Enter:
                self.label.setPixmap(self.pixmap2)
            elif event.type() == QtCore.QEvent.Leave:
                self.label.setPixmap(self.pixmap1)
        return super(Window, self).eventFilter(widget, event)  

if __name__ == '__main__':

    import sys
    app = QtGui.QApplication(sys.argv)
    window = Window()
    window.setGeometry(500, 300, 200, 200)
    window.show()
    sys.exit(app.exec_())

Upvotes: 1

Related Questions