QCustomPlot Discussion and Comments

mousePressEvent and customContextMenuRequestReturn to overview

Hello,

I would like to code a specific behavior for right click:
* if it falls on a QCPCurve, I would like to show a specifc menu (QMenu on mouse position)
* only if not, display the "normal" context menu shown in reaction to curstomContextMenuRequest

My code looks like this:

class::class { // Constructor
    setContextMenuPolicy(Qt::CustomContextMenu);
    connect(this, SIGNAL(customContextMenuRequest(const QPoint &), this, SLOT(myDefaultMenu(const QPoint &)));
}

void class::mousePressEvent(QMouseEvent * event) { // Reimplemented from base class
    if(event->button() & Qt::RightButton) {
        if(QCPCurve * curve = qobject_cast<QCPCurve *>(itemAt(event.pos())) {
            event.accept();
            return;
        }
    }
    QCustomPlot::mousePressEvent(event);
}

I see I enter up to event.accept()/return of "mousePresseEvent" function but customContextMenuRequest is thrown just after, I cannot manage to "block it". Could you help me ?

Thanks !

you shouldn't have to reimplement mousePressEvent. when you set the context menu policy to CustomContextMenu, it should do nothing but emit the customContextMenuRequest signal.

if it still is doing the regular context menu, i would reimplement (even though you shouldnt have to) QWidget::contextMenuEvent. in there you would just accept and return.

Hello Ian,

Thanks for you answer but I am not sure to understand it. According to the following code (I have added few lines to be more clear), do you expain that I should not see signal "customContextMenuRequest" thrown ?

class::class { // Constructor
    setContextMenuPolicy(Qt::CustomContextMenu);
    connect(this, SIGNAL(customContextMenuRequest(const QPoint &), this, SLOT(myDefaultMenu(const QPoint &)));
}
 
void class::myDefaultMenu(const QPoint &) {
    BUILD THE DEFAULT MENU
}

void class::mousePressEvent(QMouseEvent * event) { // Reimplemented from base class
    if(event->button() & Qt::RightButton) {
        if(QCPCurve * curve = qobject_cast<QCPCurve *>(itemAt(event.pos())) {
            event.accept();

            BUILD A SPECIFIC MENU => DEFAULT MENU SHALL NOT BE DISPLAYED

            return;
        }
    }
    QCustomPlot::mousePressEvent(event);
}

What I see is that the specific menu is build and displayed when clicking on a QCPCurve with right button. But just after, default menu is also shown (and I would like to avoid that).

okay, i see the issue here.

when you setContextMenuPolicy to Qt::CustomContextMenu, it replaces the right click event (which usually comes in the form of QWidget::contextMenuEvent(QContextMenuEvent* event) with sending out the signal customContextMenuRequest.

my guess is the issue here is that you are calling the custom menu from mousePressEvent and then when the customContextMenuRequest signal is going out, you are then creating the default menu.

i would delete your code between lines 10 and 21 and then inside your slot for the custom signal, i would implement the check at line 12 and create both menus in that function (and only exec one). this should only give you a single menu, which would be whichever one you want based on whether or not you clicked on a curve.

Thanks Ian ! Your proposal was my second choice but if there is no other solution, I will implement it as described.