Search code examples
c++qteventssubclassqpixmap

Subclassing QPixmap


I would like to get mouse press events on a QPixmap in Qt. I tried to subclass it using:

class CustomPixmap : public QPixmap
{
    Q_OBJECT

public:
    CustomPixmap(QPaintDevice *parent = NULL);
    ~CustomPixmap() {};

protected:
    void mousePressEvent(QMouseEvent *event);

};

But it does not compile because of the error

./moc_output/moc_customPixmap.cpp:52:8: error: no member named
      'staticMetaObject' in 'QPixmap'; did you mean simply 'staticMetaObject'?

Taking the Q_OBJECT out compiles fine, but the mousePressEvent is not called. How can I properly subclass a QPixmap to get mouse press events ?


Solution

  • I finally ended up in using a QPushButton:

    QPushButton *button = new QPushButton;
    button->setIcon(QIcon(myQPixmap));
    buttonWidget=MySceneClass->scene()->addWidget(button);
    QObject::connect(button, SIGNAL(clicked()),this, SLOT(clickedSlot()));