Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Disabling the "eventFilter" trigger on child elements when implementing the window movement function.



  • Hi!
    I'm trying to make the window move functionality, but there is one problem: I don't know how to disable this function on child elements for "ui->frame_hint" (three QPushButton buttons). I.e. when the cursor hits these buttons, the window " jumps". In other words, I need to move the window with the mouse cursor behind the ui->frame_hint object, except when the cursor falls on the child objects for ui->frame_hint.

    bool MainWindow::eventFilter(QObject *watched, QEvent *event)
    {
        if (watched == ui->frame_hint)
        {
            if (event->type() == QEvent::MouseButtonPress)
            {
                QMouseEvent* mouse_event = dynamic_cast<QMouseEvent*>(event);
                if (mouse_event->button() == Qt::LeftButton)
                {
                    mouseClickCoordinate = mouse_event->pos();
                    return false;
                }
            }
            else if (event->type() == QEvent::MouseMove)
            {
                QMouseEvent* mouse_event = dynamic_cast<QMouseEvent*>(event);
                if (mouse_event->buttons() & Qt::LeftButton)
                {
                    this->move(mouse_event->globalPos() - mouseClickCoordinate);
                    return false;
                }
            }
        }
        return false;
    }
    

    Thanks!


  • Lifetime Qt Champion

    Hi,

    You should return the base class implementation rather than just false. You do not allow further processing doing that.

    Note that in your implementation you return false whatever happens.



  • @SGaist

    I replaced "return false" everywhere with

    return QObject::eventFilter(watched, event);
    

    but nothing has changed, still the same problem...



  • @Helge1980

    • Did that include the final return false at the end of your eventFilter()?

    • The base class of your MainWindow is presumably QMainWindow? So you must use return QMainWindow::eventFilter(watched, event); everywhere.



  • @JonB
    Yes, there is also a return QMainWindow::eventFilter(watched, event) at the end of eventFilter ().
    Now the final code looks like this:

    bool MainWindow::eventFilter(QObject *watched, QEvent *event)
    {
        if (watched == ui->frame_hint)
        {
            if (event->type() == QEvent::MouseButtonPress)
            {
                QMouseEvent* mouse_event = dynamic_cast<QMouseEvent*>(event);
                if (mouse_event->button() == Qt::LeftButton)
                {
                    mouseClickCoordinate = mouse_event->pos();
                    return QMainWindow::eventFilter(watched, event);
                }
            }
            else if (event->type() == QEvent::MouseMove)
            {
                QMouseEvent* mouse_event = dynamic_cast<QMouseEvent*>(event);
                if (mouse_event->buttons() & Qt::LeftButton)
                {
                    this->move(mouse_event->globalPos() - mouseClickCoordinate);
                    return QMainWindow::eventFilter(watched, event);
                }
            }
        }
        return QMainWindow::eventFilter(watched, event);
    }
    


  • It was obtained using an additional variable

    bool clickPressedFlag = false;
    

    and using

    QEvent::MouseButtonRelease
    

    Working code:

    bool MainWindow::eventFilter(QObject *watched, QEvent *event)
    {
        if (event->type() == QEvent::MouseButtonRelease)
        {
            QMouseEvent* mouse_event = dynamic_cast<QMouseEvent*>(event);
            if (mouse_event->button() == Qt::LeftButton)
            {
                clickPressedFlag = false;
                return QMainWindow::eventFilter(watched, event);
            }
        }
    
        if (watched == ui->frame_top_btns)
        {
            if (event->type() == QEvent::MouseButtonPress)
            {
                QMouseEvent* mouse_event = dynamic_cast<QMouseEvent*>(event);
                if (mouse_event->button() == Qt::LeftButton)
                {
                    mouseClickCoordinate = mouse_event->pos();
                    clickPressedFlag = true;
                    return QMainWindow::eventFilter(watched, event);
                }
            }
            else if ((event->type() == QEvent::MouseMove) && clickPressedFlag == true)
            {
                QMouseEvent* mouse_event = dynamic_cast<QMouseEvent*>(event);
                if (mouse_event->buttons() & Qt::LeftButton)
                {
                    this->move(mouse_event->globalPos() - mouseClickCoordinate);
                    return QMainWindow::eventFilter(watched, event);
                }
            }
        }
        return QMainWindow::eventFilter(watched, event);
    }
    

Log in to reply