How to get mouse press event on a QPixmap



  • I have a QPixmap that I add to a QGraphicsScene, which gives me a QGraphicsPixmapItem.
    How to get a mousePress event on the QGraphicsPixmapItem ?
    Tried to subclass the QGraphicsPixmapItem, but this gives me nothing.

    Thanks.


  • Qt Champions 2016

    @EarthHobbit
    Hi

    This sample works for me
    (i can click it )

    #include <QApplication>
    #include<QGraphicsScene>
    #include<QGraphicsDropShadowEffect>
    #include<QGraphicsPixmapItem>
    #include<QMessageBox>
    #include<QGraphicsView>
    
    class myGraphicsPixmapItem: public QGraphicsPixmapItem {
     public:
      myGraphicsPixmapItem(QPixmap pixmap): QGraphicsPixmapItem(pixmap) {
      };
    
      ~myGraphicsPixmapItem() {
      };
    
      void mouseReleaseEvent(QGraphicsSceneMouseEvent* event) {
        QMessageBox::information(NULL, "Information!", "Mouse release Detected!");
      };
    };
    
    
    int main(int argc, char* argv[]) {
      QApplication app(argc, argv);
      QGraphicsScene scene;
      QPixmap pix(200, 200);
      QPainter* paint = new QPainter(&pix);
      paint->setPen(QColor(255, 34, 255, 255));
      paint->drawRect(0, 0, 200, 200);
      myGraphicsPixmapItem* pixmapItem = new myGraphicsPixmapItem(pix);
      pixmapItem->setFlags(QGraphicsItem::ItemIsMovable);
      delete paint;
      QGraphicsView view(&scene);
      scene.addItem(pixmapItem);
      view.showFullScreen();
      return app.exec();
    }
    


Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.