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. [macOS] How to hide the mouse cursor while typing in a text box?
Forum Updated to NodeBB v4.3 + New Features

[macOS] How to hide the mouse cursor while typing in a text box?

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 2 Posters 870 Views 2 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.
  • K Offline
    K Offline
    Kerndog73
    wrote on 2 Oct 2019, 08:02 last edited by
    #1

    On macOS, the mouse is hidden when you start typing into a text box. The mouse is shown again after you move it. This is to stop the mouse from covering the text you're trying to type. I'm trying to reproduce this in my Qt app but I'm having a bit of trouble. Here's a gif that demonstrates the desired behaviour using a native mac app (iTerm2).

    Demo

    First off, I don't know how to globally hide the mouse cursor. QApplication::setOverrideCursor(Qt::BlankCursor); isn't quite enough. It will hide the cursor when it's within the window but not when it's outside the window.

    Secondly, I don't know if it's possible to get mouse events when the mouse is outside the window. It seems like I'll probably have to write some Objective-C to get this work. Who knows, maybe there's a hideTheMouseUntilItMoves function buried in Cocoa somewhere!

    I'm wondering if anyone that's familiar with native mac development knows of an easy way to accomplish this. Should I just settle for setCursor?

    1 Reply Last reply
    0
    • K Offline
      K Offline
      Kerndog73
      wrote on 2 Oct 2019, 08:28 last edited by
      #2

      This has just blown my mind!

      There isn't a function called hideTheMouseUntilItMoves but there is setHiddenUntilMouseMoves that does exactly what I want it to do!

      All I had to do was write this Objective-C++ code:

      #import <AppKit/NSCursor.h>
      
      void hideMouseUntilMouseMoves() {
        [NSCursor setHiddenUntilMouseMoves:true];
      }
      

      Then I just call that function when a key is pressed.

      #include <QtWidgets/qlineedit.h>
      #include <QtWidgets/qmainwindow.h>
      #include <QtWidgets/qapplication.h>
      
      void hideMouseUntilMouseMoves();
      
      class LineEdit final : public QLineEdit {
      public:
        explicit LineEdit(QWidget *parent)
          : QLineEdit{parent} {}
          
      private:
        void keyPressEvent(QKeyEvent *event) override {
          QLineEdit::keyPressEvent(event);
          hideMouseUntilMouseMoves();
        }
      };
      
      int main(int argc, char **argv) {
        QApplication app{argc, argv};
        QMainWindow window;
        LineEdit box{&window};
        window.show();
        return app.exec();
      }
      

      I definitely feel that this should be a feature of Qt. I might post a feature request to the bug tracker.

      1 Reply Last reply
      4
      • K Offline
        K Offline
        Kerndog73
        wrote on 2 Oct 2019, 08:56 last edited by Kerndog73 10 Feb 2019, 08:56
        #3

        I made a post on the bug reporter.

        1 Reply Last reply
        3
        • S Offline
          S Offline
          SGaist
          Lifetime Qt Champion
          wrote on 2 Oct 2019, 09:35 last edited by
          #4

          Hi,

          Nice one ! Thanks for sharing !

          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
          1

          1/4

          2 Oct 2019, 08:02

          • Login

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