Search code examples
c++qtuser-interfaceevent-handlingqmouseevent

Filtering Mouse clicks in Qt within a class


I want to be able to enable and disable filtering mouse clicks on my entire QMainWindow by pressing a button or a key which will cause filtering to start. I want to enable the event filter from inside a class, QMainWindow.

I want to be able to have a eventfilter inside my class we can call MyWindow, from what i've found i'm supposed to make a new class MouseFilter, redefine eventFilter(QObject* object,QEvent* event) and install it on myWindow

int main(int argc, char *argv[]) {
    QApplication a(argc, argv);
    MyWindow w;
    w.installEventFilter(new MouseFilter());
    w.show();
    return a.exec();
}

Is there a way I can implement the event filter from inside my object?


Solution

I think I may have been over-complicating things. It as simple as subclassing mousePressEvent.

void MouseFilter::mousePressEvent(QMouseEvent * event){
    if(event->button() == Qt::RightButton){
        qDebug() << "Right-o";
    }
}

Solution

  • I think what you're looking for is the mousePressEvent which you can override from within MyWindow

    Cheers, Rostislav.