1

I've extended the QgsMapCanvas to catch mouse events and all works well. Then I try to catch events from a QgsMapCanvasItem. Reading the classes QgsMapCanvas extends QgraphicsView, and QgsMapCanvasItem extends a QGraphicsItem so in Qt is very simple to pass events from QgraphicsView to QGraphicsItem , but it seems impossible from Qgis using C++

Here is my code:

    class HciCanvas : public QgsMapCanvas
{

  Q_OBJECT

public:
    HciCanvas(QWidget * grafD);
    ~HciCanvas();

    void mousePressEvent(QMouseEvent *event) override;

and in .CPP

void HciCanvas::mousePressEvent(QMouseEvent *event){
qDebug() << "HciCanvas::mousePressEvent in screen X:" << event->pos().x() << " Y: " << event->pos().y();
    QGraphicsView::mousePressEvent(event);  // to propagate events !!!!
    QgsMapCanvas::mousePressEvent(event);
}

Then the graphics Item :

class PpiItem : public QgsMapCanvasItem
{

public:
    PpiItem(QgsMapCanvas * canv, QPixmap * pixmap);
    ~PpiItem();

    void setCenter(const QgsPointXY &point);
    QgsPointXY center() const { return _pixmapSourceCenter; };

   void mousePressEvent(QGraphicsSceneMouseEvent *event) override;

and in CPP

void PpiItem::mousePressEvent(QGraphicsSceneMouseEvent *event){
    qDebug("PpiItem::mousePressEvent");
}

Finally in mainwindow.cpp

pixmap = new QPixmap(":/img/boat.png");
     myMapCanvas = new HciCanvas(this);
     myMapCanvas->resize(QSize(1280,1024));
     myMapCanvas->enableAntiAliasing(true);
     myMapCanvas->setCanvasColor(QColor(189,210,255));   //background
     myMapCanvas->freeze(false);
     myMapCanvas->setVisible(true);

 
    // set canvaswidget
     this->setCentralWidget(myMapCanvas);

     //loading world shape
     LoadShapeWorldLayer();

     //test loading a raster
     LoadRasterLayer();

     //Add layers list to MAP LAYER REGISTRY
     QgsProject::instance()->addMapLayers(myMapLayers);

     // Set the Map Canvas Layer Set
     myMapCanvas->setLayers(myMapLayers);


     item = new PpiItem(myMapCanvas, pixmap);
     QPointF centre;
     centre.setX(9);
     centre.setY(44);
     item->setCenter(centre);

This code produces a Framework with a map. If I click on the map the events in HciCanvas is catch and I see the call to in log "HciCanvas::mousePressEvent..." If I click on the PpiItem ( the boat.png) I see only the call to HciCanvas::mousePressEvent but not the call to PpiItem::mousePressEvent.

why this strange behavior?

Look the code below ! It works like charme in Qt so i think it is a QGIS problem !

#include <QApplication>
#include <QGraphicsView>
#include <QDebug>
#include <QGraphicsItem>

class CustomView : public QGraphicsView
{
protected:
    void mousePressEvent(QMouseEvent *event)
    {
        qDebug() << "Custom view clicked.";
        QGraphicsView::mousePressEvent(event);
    }
};


class CustomItem : public QGraphicsItem
{
public:

    CustomItem(QString n, int v ){ name = n; value = v;};

    QRectF boundingRect() const override
    {
        qreal penWidth = 1;
        return QRectF(-value - penWidth / 2, -10 - penWidth / 2,
                      20 + penWidth, 20 + penWidth);
    }

    void paint(QPainter *painter, const QStyleOptionGraphicsItem *option,
               QWidget *widget) override
    {
        painter->drawRoundedRect(-value, -10, 20, 20, 5, 5);
    }



protected:
    void mousePressEvent(QGraphicsSceneMouseEvent *event) override
    {
        qDebug() << "Custom item clicked." << name;


    }

   private:

    QString name;
    int value;
};



int main(int argc, char *argv[])
{
    QApplication a(argc, argv);

    CustomItem item("Asko",10);


    QGraphicsScene scene(0, 0, 100, 100);
    scene.addItem(&item);

    CustomItem item2("Jeremias",70);
    scene.addItem(&item2);

    CustomView view;
    view.setScene(&scene);
    view.show();
    return a.exec();
}
2
  • Perhaps this answer will be helpful to you although for Python not C++ gis.stackexchange.com/questions/442985/…
    – Ben W
    Commented Oct 29, 2022 at 0:53
  • see this post, but consider that using C++ .. this code works like a charm, so I think it is a Qgis Problem ! Look the code I've added to the last part of the question...
    – Gab74
    Commented Oct 31, 2022 at 8:31

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.