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
Forum Updated to NodeBB v4.3 + New Features

Restrict mouse cursor movement

Scheduled Pinned Locked Moved Solved General and Desktop
widgetsmain windowmousemouse control
14 Posts 5 Posters 14.3k Views 3 Watching
  • 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.
  • mrjjM Offline
    mrjjM Offline
    mrjj
    Lifetime Qt Champion
    wrote on 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?

    kshegunovK mrjjM 2 Replies Last reply
    0
    • mrjjM mrjj

      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?

      kshegunovK Offline
      kshegunovK Offline
      kshegunov
      Moderators
      wrote on 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
      • mrjjM mrjj

        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?

        mrjjM Offline
        mrjjM Offline
        mrjj
        Lifetime Qt Champion
        wrote on 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.

        kshegunovK 1 Reply Last reply
        0
        • R Offline
          R Offline
          Rahul Sharma
          wrote on 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
          • mrjjM mrjj

            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.

            kshegunovK Offline
            kshegunovK Offline
            kshegunov
            Moderators
            wrote on 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

            mrjjM 1 Reply Last reply
            0
            • kshegunovK kshegunov

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

              mrjjM Offline
              mrjjM Offline
              mrjj
              Lifetime Qt Champion
              wrote on 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.

              kshegunovK 1 Reply Last reply
              0
              • mrjjM mrjj

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

                kshegunovK Offline
                kshegunovK Offline
                kshegunov
                Moderators
                wrote on 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

                mrjjM 1 Reply Last reply
                0
                • kshegunovK kshegunov

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

                  mrjjM Offline
                  mrjjM Offline
                  mrjj
                  Lifetime Qt Champion
                  wrote on 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 Online
                    Chris KawaC Online
                    Chris Kawa
                    Lifetime Qt Champion
                    wrote on 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.

                    kshegunovK 1 Reply Last reply
                    0
                    • Chris KawaC Chris Kawa

                      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.

                      kshegunovK Offline
                      kshegunovK Offline
                      kshegunov
                      Moderators
                      wrote on 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

                      kshegunovK 1 Reply Last reply
                      0
                      • kshegunovK kshegunov

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

                        kshegunovK Offline
                        kshegunovK Offline
                        kshegunov
                        Moderators
                        wrote on 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
                        • mrjjM Offline
                          mrjjM Offline
                          mrjj
                          Lifetime Qt Champion
                          wrote on last edited by
                          #13

                          Ok, that i completely missed it had :)

                          1 Reply Last reply
                          0
                          • W Offline
                            W Offline
                            wuzn
                            wrote on 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