Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Disabling the "eventFilter" trigger on child elements when implementing the window movement function.

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

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 3 Posters 362 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H Offline
    H Offline
    Helge1980
    wrote on last edited by Helge1980
    #1

    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!

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      H 1 Reply Last reply
      1
      • SGaistS SGaist

        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.

        H Offline
        H Offline
        Helge1980
        wrote on last edited by
        #3

        @SGaist

        I replaced "return false" everywhere with

        return QObject::eventFilter(watched, event);
        

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

        JonBJ 1 Reply Last reply
        0
        • H Helge1980

          @SGaist

          I replaced "return false" everywhere with

          return QObject::eventFilter(watched, event);
          

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

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by JonB
          #4

          @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.

          H 1 Reply Last reply
          1
          • JonBJ JonB

            @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.

            H Offline
            H Offline
            Helge1980
            wrote on last edited by
            #5

            @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);
            }
            
            1 Reply Last reply
            0
            • H Offline
              H Offline
              Helge1980
              wrote on last edited by Helge1980
              #6

              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);
              }
              
              1 Reply Last reply
              0

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved