Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. General talk
  3. Qt 6
  4. How to prevent a window from being moved?
QtWS25 Last Chance

How to prevent a window from being moved?

Scheduled Pinned Locked Moved Unsolved Qt 6
7 Posts 3 Posters 202 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.
  • M Offline
    M Offline
    Mike Christensen
    wrote on 26 Mar 2025, 07:43 last edited by
    #1

    I have the following class:

    class MyWindow : public QWidget {
       public:
        MyWindow() {
            setWindowFlags(Qt::FramelessWindowHint | Qt::WindowStaysOnTopHint);
            setStyleSheet("background-color: black;");
    
            QScreen *screen = QGuiApplication::primaryScreen();
            int screenWidth = screen->size().width();
    
            setFixedSize(screenWidth, 50);
        }
    
       protected:
        // Override the paintEvent to draw custom text
        void paintEvent(QPaintEvent *event) override {
            QPainter painter(this);
            painter.setPen(Qt::white);                     // Set text color to black
            painter.setFont(QFont("JetBrains Mono", 15));  // Set font
    
            // Draw the text in the center of the window
            painter.drawText(rect(), Qt::AlignCenter, "Hello World");
        }
    };
    

    I would like to prevent the window from being moved no matter what. Is there any property I can set on the window to make it immovable, or any event I can trap and cancel? I looked through the docs and didn't find anything. There's a few StackOverflow posts from like 2012 but none of the suggested answers work. Thanks!

    J 1 Reply Last reply 26 Mar 2025, 07:56
    0
    • M Mike Christensen
      26 Mar 2025, 07:43

      I have the following class:

      class MyWindow : public QWidget {
         public:
          MyWindow() {
              setWindowFlags(Qt::FramelessWindowHint | Qt::WindowStaysOnTopHint);
              setStyleSheet("background-color: black;");
      
              QScreen *screen = QGuiApplication::primaryScreen();
              int screenWidth = screen->size().width();
      
              setFixedSize(screenWidth, 50);
          }
      
         protected:
          // Override the paintEvent to draw custom text
          void paintEvent(QPaintEvent *event) override {
              QPainter painter(this);
              painter.setPen(Qt::white);                     // Set text color to black
              painter.setFont(QFont("JetBrains Mono", 15));  // Set font
      
              // Draw the text in the center of the window
              painter.drawText(rect(), Qt::AlignCenter, "Hello World");
          }
      };
      

      I would like to prevent the window from being moved no matter what. Is there any property I can set on the window to make it immovable, or any event I can trap and cancel? I looked through the docs and didn't find anything. There's a few StackOverflow posts from like 2012 but none of the suggested answers work. Thanks!

      J Offline
      J Offline
      JonB
      wrote on 26 Mar 2025, 07:56 last edited by
      #2

      @Mike-Christensen
      I would not have thought so, that is up to your desktop/window manager (like Wayland), not your application. I certainly hope not, else applications could totally restrict my use of the desktop by making their windows immovable and drive me mad.

      J 1 Reply Last reply 26 Mar 2025, 08:02
      0
      • J JonB
        26 Mar 2025, 07:56

        @Mike-Christensen
        I would not have thought so, that is up to your desktop/window manager (like Wayland), not your application. I certainly hope not, else applications could totally restrict my use of the desktop by making their windows immovable and drive me mad.

        J Offline
        J Offline
        J.Hilk
        Moderators
        wrote on 26 Mar 2025, 08:02 last edited by
        #3

        @JonB weeelll, I could certainly make it very difficult and annoying for you to move/hide my window if I wanted to. And like 90% of all windows users would be unable to circumvent that.

        @Mike-Christensen make a QTimer with like a 5 ms cool down, move(x,y) on the timeout to the position where you want it.

        I feel kinda dirty right now.


        Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


        Q: What's that?
        A: It's blue light.
        Q: What does it do?
        A: It turns blue.

        J 1 Reply Last reply 26 Mar 2025, 08:14
        1
        • J J.Hilk
          26 Mar 2025, 08:02

          @JonB weeelll, I could certainly make it very difficult and annoying for you to move/hide my window if I wanted to. And like 90% of all windows users would be unable to circumvent that.

          @Mike-Christensen make a QTimer with like a 5 ms cool down, move(x,y) on the timeout to the position where you want it.

          I feel kinda dirty right now.

          J Offline
          J Offline
          JonB
          wrote on 26 Mar 2025, 08:14 last edited by
          #4

          @J-Hilk
          Thankfully Wayland at least will prevent you from positioning your window where you want it.

          You should indeed feel dirty under Windows! Thank goodness I am not aware of any Windows application I use where I choose to move a window and then it moves itself back to where it was afterwards. Such behaviour would be really annoying so i would try not to use your application!

          1 Reply Last reply
          1
          • M Offline
            M Offline
            Mike Christensen
            wrote on 26 Mar 2025, 18:13 last edited by
            #5

            @JonB @J-Hilk - Hah, thanks for the help. This is actually part of my effort to create a dock so it should behave more like a dock and not a window. It seems everything about Qt and Wayland make that sort of thing very difficult to do.

            I guess one idea would be to make the dock technically movable, but it would "snap" to the nearest screen edge or something.. Anyway, something to look into more. Thanks!

            J 1 Reply Last reply 26 Mar 2025, 18:59
            0
            • M Mike Christensen
              26 Mar 2025, 18:13

              @JonB @J-Hilk - Hah, thanks for the help. This is actually part of my effort to create a dock so it should behave more like a dock and not a window. It seems everything about Qt and Wayland make that sort of thing very difficult to do.

              I guess one idea would be to make the dock technically movable, but it would "snap" to the nearest screen edge or something.. Anyway, something to look into more. Thanks!

              J Offline
              J Offline
              JonB
              wrote on 26 Mar 2025, 18:59 last edited by
              #6

              @Mike-Christensen said in How to prevent a window from being moved?:

              It seems everything about Qt and Wayland make that sort of thing very difficult to do.

              That is certainly true. But surely that is down to Wayland, not Qt. I now see you are commenting on that in your other thread. FWIW, I have reverted my Ubuntu to Xorg, it wasn't just because of Qt at all, other applications had problems, and I am not alone in this.

              M 1 Reply Last reply 26 Mar 2025, 19:54
              0
              • J JonB
                26 Mar 2025, 18:59

                @Mike-Christensen said in How to prevent a window from being moved?:

                It seems everything about Qt and Wayland make that sort of thing very difficult to do.

                That is certainly true. But surely that is down to Wayland, not Qt. I now see you are commenting on that in your other thread. FWIW, I have reverted my Ubuntu to Xorg, it wasn't just because of Qt at all, other applications had problems, and I am not alone in this.

                M Offline
                M Offline
                Mike Christensen
                wrote on 26 Mar 2025, 19:54 last edited by
                #7

                @JonB Yea, I thought writing a dock/taskbar "Waybar replacement" might be a fun weekend project and a chance to get back into C++ and learn Qt. I never imagined this rabbit hole. Anyway, I guess Wayland is still [relatively?] new and hopefully there will be solutions for these use cases as more major distros go Wayland native. It does give me respect for those implementing entire desktop environments such as KDE Plasma on Wayland since it's now clear all the fire hoops they have to jump through to get this stuff working.

                1 Reply Last reply
                0

                6/7

                26 Mar 2025, 18:59

                • Login

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