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. mouseMoveEvent not being called
QtWS25 Last Chance

mouseMoveEvent not being called

Scheduled Pinned Locked Moved Solved General and Desktop
mouseeventsmousemoveevent-handlingqframeframe
5 Posts 3 Posters 2.3k 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.
  • S Offline
    S Offline
    Snowdrop
    wrote on 20 Aug 2021, 19:55 last edited by
    #1

    I have subclassed QFrame, set mouse tracking to true, and reimplemented mouseMoveEvent(...), but the function is only called when the mouse enters the frame, exits the frame, or is clicked inside the frame. I have tried setting the focus policy on the frame to StrongFocus, and setting it to NoFocus for all its child widgets, but that had no effect. It looks like the event is captured by some other widget and blocked, but I can't figure out where.

    I am using Qt 5.15, this problem is present in Windows, Linux and macOS

    1 Reply Last reply
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 20 Aug 2021, 19:59 last edited by
      #2

      Hi,

      Please post a minimal compilable example that shows that behaviour.

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

      1 Reply Last reply
      0
      • S Offline
        S Offline
        Snowdrop
        wrote on 20 Aug 2021, 20:41 last edited by
        #3

        I managed to find a solution... I overrode eventFilter(...) in my QFrame subclass:

        bool MouseTrackingFrame::eventFilter(QObject *watched, QEvent *event)
        {
            static int c = 0;
            qDebug() << "EVENT: " << ++c << " - " << event->type();
            if (event->type() == QEvent::MouseMove)
            {
                mouseMoveEvent(static_cast<QMouseEvent *>(event) );
                return false;
            }
        
            return QFrame::eventFilter(watched, event);
        }
        

        Then I recursively installed it to all its child widgets, as well as enabling mouse tracking like this:

        void install_filter(QObject *target, QObject *filter)
        {
            if (target->isWidgetType() )
            {
                qDebug("INSTALL");
                static_cast<QWidget *>(target)->setMouseTracking(true);
                target->installEventFilter(filter);
            }
            const QObjectList &children = target->children();
            for (auto i = children.begin(); i < children.end(); i++)
            {
                install_filter(*i, filter);
            }
        }
        
        void MouseTrackingFrame::postSetup()
        {
            install_filter(this, this);
        }
        
        
        ...
        
        MainWindow::MainWindow(QWidget *parent)
            : QMainWindow(parent) ...
        {
            ui->setupUi(this);
            ui->mouseTrackingFrame->postSetup();
            ...
        }
        
        1 Reply Last reply
        0
        • J Online
          J Online
          JoeCFD
          wrote on 20 Aug 2021, 21:41 last edited by
          #4

          do you have the following line in the constructor of class MouseTrackingFrame?
          installEventFilter( this );

          S 1 Reply Last reply 21 Aug 2021, 01:08
          0
          • J JoeCFD
            20 Aug 2021, 21:41

            do you have the following line in the constructor of class MouseTrackingFrame?
            installEventFilter( this );

            S Offline
            S Offline
            Snowdrop
            wrote on 21 Aug 2021, 01:08 last edited by
            #5

            @JoeCFD said in mouseMoveEvent not being called:

            do you have the following line in the constructor of class MouseTrackingFrame?
            installEventFilter( this );

            No, the install_filter(...) function being called from MouseTrackingFrame::postSetup() does that. But I'm not sure if it has any effect - I saw a post somewhere which said Qt ignores when the widget itself is passed to installEventFilter(...)

            1 Reply Last reply
            0

            2/5

            20 Aug 2021, 19:59

            • Login

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