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. Restrict mouse cursor movement
QtWS25 Last Chance

Restrict mouse cursor movement

Scheduled Pinned Locked Moved Solved General and Desktop
widgetsmain windowmousemouse control
14 Posts 5 Posters 14.1k 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.
  • K Offline
    K Offline
    kshegunov
    Moderators
    wrote on 17 Dec 2015, 07:03 last edited by
    #1

    Hello,
    I have a widgets application and I'd want to restrict the mouse from leaving my QMainWindow. I have considered grabbing the mouse, but it doesn't really suit me, as I still want to receive events (in the child widgets), but I don't want to allow the mouse to leave the window. Any suggestion on how such a behavior can be achieved?

    Kind regards.

    Read and abide by the Qt Code of Conduct

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mrjj
      Lifetime Qt Champion
      wrote on 17 Dec 2015, 07:53 last edited by
      #2

      Hi
      Do you mean something like this windows function but crossplatform ?
      https://msdn.microsoft.com/en-us/library/windows/desktop/ms648383(v=vs.85).aspx

      And Mainwindow cannot just be fullscreen?

      K M 2 Replies Last reply 17 Dec 2015, 08:02
      0
      • M mrjj
        17 Dec 2015, 07:53

        Hi
        Do you mean something like this windows function but crossplatform ?
        https://msdn.microsoft.com/en-us/library/windows/desktop/ms648383(v=vs.85).aspx

        And Mainwindow cannot just be fullscreen?

        K Offline
        K Offline
        kshegunov
        Moderators
        wrote on 17 Dec 2015, 08:02 last edited by
        #3

        Hello,

        Do you mean something like this windows function but crossplatform?

        Yes I mean it exactly like that.

        And Mainwindow cannot just be fullscreen?

        It is in a pseudo full screen because of a bug in Qt (I have opengl widget embeded that prevents full screen from working properly). Additionally, even if put in full screen the cursor is not clipped to an area, because there are multiple screens, so Qt allows the cursor to escape (which is of course an expected behavior).

        Kind regards.

        Read and abide by the Qt Code of Conduct

        1 Reply Last reply
        0
        • M mrjj
          17 Dec 2015, 07:53

          Hi
          Do you mean something like this windows function but crossplatform ?
          https://msdn.microsoft.com/en-us/library/windows/desktop/ms648383(v=vs.85).aspx

          And Mainwindow cannot just be fullscreen?

          M Offline
          M Offline
          mrjj
          Lifetime Qt Champion
          wrote on 17 Dec 2015, 08:15 last edited by
          #4

          Ok.
          I didn't find any good way.
          I tried using MouseMoveEvent + mousetracking
          but if you moved the mouse really fast
          it would not detect leaving the window.

          K 1 Reply Last reply 17 Dec 2015, 18:54
          0
          • R Offline
            R Offline
            Rahul Sharma
            wrote on 17 Dec 2015, 08:17 last edited by
            #5

            @kshegunov said:

            may be you can get some information from this-
            http://www.codeprogress.com/cpp/libraries/qt/QtHideMouseCursor.php

            1 Reply Last reply
            0
            • M mrjj
              17 Dec 2015, 08:15

              Ok.
              I didn't find any good way.
              I tried using MouseMoveEvent + mousetracking
              but if you moved the mouse really fast
              it would not detect leaving the window.

              K Offline
              K Offline
              kshegunov
              Moderators
              wrote on 17 Dec 2015, 18:54 last edited by
              #6

              @mrjj
              Right? Me neither. The only thing I could fathom was to grab the mouse, but then my child widgets don't get anything. Maybe I could manually dispatch the messages, but it does seem weird.

              @Rahul-Sharma
              Sorry, but does me no good. I'm not after changing the cursor image, but disallowing it to leave a certain area instead. Thanks for the suggestion, though.

              Read and abide by the Qt Code of Conduct

              M 1 Reply Last reply 17 Dec 2015, 18:59
              0
              • K kshegunov
                17 Dec 2015, 18:54

                @mrjj
                Right? Me neither. The only thing I could fathom was to grab the mouse, but then my child widgets don't get anything. Maybe I could manually dispatch the messages, but it does seem weird.

                @Rahul-Sharma
                Sorry, but does me no good. I'm not after changing the cursor image, but disallowing it to leave a certain area instead. Thanks for the suggestion, though.

                M Offline
                M Offline
                mrjj
                Lifetime Qt Champion
                wrote on 17 Dec 2015, 18:59 last edited by
                #7

                @kshegunov
                Not sure how much worked there is to self dispatch messages.
                My use case for for demo to disallow people go clicking on other tings, so i ended up
                with timer, moving the mouse back if it has moved outside.
                While not limiting it for real. it was impossible to click on stuff outside.
                Still not a good solution but fine for the 2 hours demo.

                What is your use case`? if i may ask.

                K 1 Reply Last reply 17 Dec 2015, 19:31
                0
                • M mrjj
                  17 Dec 2015, 18:59

                  @kshegunov
                  Not sure how much worked there is to self dispatch messages.
                  My use case for for demo to disallow people go clicking on other tings, so i ended up
                  with timer, moving the mouse back if it has moved outside.
                  While not limiting it for real. it was impossible to click on stuff outside.
                  Still not a good solution but fine for the 2 hours demo.

                  What is your use case`? if i may ask.

                  K Offline
                  K Offline
                  kshegunov
                  Moderators
                  wrote on 17 Dec 2015, 19:31 last edited by
                  #8

                  @mrjj
                  Sure, I don't like secrets, that's why I prefer Linux ;P
                  It's for my pet project - a proof of concept Qt based game. I think I've mentioned it to you. I need to restrict the mouse from leaving the main window, because there is an action I wish to attach to certain area near the edges of the window, namely to allow the player to move around the world with the mouse.

                  Read and abide by the Qt Code of Conduct

                  M 1 Reply Last reply 17 Dec 2015, 19:41
                  0
                  • K kshegunov
                    17 Dec 2015, 19:31

                    @mrjj
                    Sure, I don't like secrets, that's why I prefer Linux ;P
                    It's for my pet project - a proof of concept Qt based game. I think I've mentioned it to you. I need to restrict the mouse from leaving the main window, because there is an action I wish to attach to certain area near the edges of the window, namely to allow the player to move around the world with the mouse.

                    M Offline
                    M Offline
                    mrjj
                    Lifetime Qt Champion
                    wrote on 17 Dec 2015, 19:41 last edited by mrjj
                    #9

                    @kshegunov
                    hehe.
                    Yes you mentioned pet :)
                    So In reality if opengl would go fullscreen it would just work.
                    Else I think grab is the best option.

                    1 Reply Last reply
                    0
                    • Chris KawaC Offline
                      Chris KawaC Offline
                      Chris Kawa
                      Lifetime Qt Champion
                      wrote on 17 Dec 2015, 20:49 last edited by
                      #10

                      Some games do it this way:

                      • Hide the cursor (set to Qt::BlankCursor)
                      • In mouse move event record a distance of the cursor from the center of your window (QCursor::pos() and window geometry). Use that distance to update your "virtual cursor" clipped to whatever region you want.
                      • Move the cursor to the center of your window
                      • Draw your own cursor "in game" at the "virtual" position

                      In mouse clicks use your "virtual" position instead of the real one. If you also need to pass the moves to some widgets etc. then you can filter your window for mouse clicks, intercept and resend the events with your "virtual" position.

                      K 1 Reply Last reply 17 Dec 2015, 21:04
                      0
                      • Chris KawaC Chris Kawa
                        17 Dec 2015, 20:49

                        Some games do it this way:

                        • Hide the cursor (set to Qt::BlankCursor)
                        • In mouse move event record a distance of the cursor from the center of your window (QCursor::pos() and window geometry). Use that distance to update your "virtual cursor" clipped to whatever region you want.
                        • Move the cursor to the center of your window
                        • Draw your own cursor "in game" at the "virtual" position

                        In mouse clicks use your "virtual" position instead of the real one. If you also need to pass the moves to some widgets etc. then you can filter your window for mouse clicks, intercept and resend the events with your "virtual" position.

                        K Offline
                        K Offline
                        kshegunov
                        Moderators
                        wrote on 17 Dec 2015, 21:04 last edited by kshegunov
                        #11

                        @Chris-Kawa
                        Thanks for the suggestions, I'll think about it. They all seem to revolve around remapping the cursor, which I'm not sure will work for me, because I have Qt3D put into a QOpenGLWindow and it handles the mouse events itself. Still, thank you again for taking the time.

                        Kind regards.

                        Read and abide by the Qt Code of Conduct

                        K 1 Reply Last reply 18 Dec 2015, 14:20
                        0
                        • K kshegunov
                          17 Dec 2015, 21:04

                          @Chris-Kawa
                          Thanks for the suggestions, I'll think about it. They all seem to revolve around remapping the cursor, which I'm not sure will work for me, because I have Qt3D put into a QOpenGLWindow and it handles the mouse events itself. Still, thank you again for taking the time.

                          Kind regards.

                          K Offline
                          K Offline
                          kshegunov
                          Moderators
                          wrote on 18 Dec 2015, 14:20 last edited by
                          #12

                          Hello,
                          It turned out to be incredibly simple in the end. There is a leave event (that was news for me) fired from Qt, to which it's possible to attach the necessary code (for anyone that might come to the same issue):

                          void MyMainWindow::leaveEvent(QEvent * event)
                          {
                          	// Get the window geometry & cursor position
                          	const QRect & rect = geometry();
                          	QPoint position = QCursor::pos();
                          
                          	// Check the bounds
                          	qint32 x = qBound(rect.left(), position.x(), rect.right());
                          	qint32 y = qBound(rect.top(), position.y(), rect.bottom());
                          
                          	// Adjust the cursor
                          	if (x != position.x() || y != position.y())
                          		QCursor::setPos(x, y);
                          
                          	event->accept();
                          	QMainWindow::leaveEvent(event);
                          }
                          

                          Thanks to all for the help.

                          Kind regards.

                          Read and abide by the Qt Code of Conduct

                          1 Reply Last reply
                          4
                          • M Offline
                            M Offline
                            mrjj
                            Lifetime Qt Champion
                            wrote on 18 Dec 2015, 14:23 last edited by
                            #13

                            Ok, that i completely missed it had :)

                            1 Reply Last reply
                            0
                            • W Offline
                              W Offline
                              wuzn
                              wrote on 13 Apr 2017, 10:26 last edited by JKSH
                              #14

                              more cursors

                              [EDIT: Irrelevant link removed --JKSH]

                              1 Reply Last reply
                              -1

                              • Login

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