2

I want to call some functions when I click left and right on the canvas. I wrote the following code for this:

mpMapCanvas = new QgsMapCanvas();
mpClickPoint = new QgsMapToolEmitPoint(mpMapCanvas);
connect(mpClickPoint, SIGNAL(canvasClicked(QgsPointXY,Qt::MouseButton)), this, SLOT(selectCoord(QgsPointXY)));

This code works, but for both left and right mouse click. So there is no difference between them right now. Either left or right click is calling selectCoord() function. I want to separate them and make it work only when I click left. That's why I tried:

connect(mpClickPoint, SIGNAL(canvasPressEvent(QgsMapMouseEvent)), this, SLOT(mouseEvent(QgsMapMouseEvent)));
void MainWindow::mouseEvent(QMouseEvent *event){
    if (event->button() == Qt::LeftButton){
        qDebug() << "Left clicked" << '\n';
    }
}

I don't know why it didn't work. Any idea?

1 Answer 1

1

I solved this issue with the following change:

connect(mpClickPoint, SIGNAL(canvasClicked(QgsPointXY,Qt::MouseButton)), this, SLOT(mouseEvent(QgsPointXY,Qt::MouseButton)));
    
void MainWindow::mouseEvent(QgsPointXY point, Qt::MouseButton button){
        if (button == Qt::LeftButton){
            // do some stuff
        }
}

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.