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. How to center mainWindow on the screen?
Forum Updated to NodeBB v4.3 + New Features

How to center mainWindow on the screen?

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 4 Posters 848 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.
  • T Offline
    T Offline
    Tusen183
    wrote on last edited by
    #1

    OS:ubuntu22
    QT6
    I've tried various codes, but my mainWindow is always in the top left corner of my screen.

    JonBJ 1 Reply Last reply
    0
    • Kent-DorfmanK Offline
      Kent-DorfmanK Offline
      Kent-Dorfman
      wrote on last edited by
      #2

      window managers can override client position hints, but

      QWidget provide move() method.

      I light my way forward with the fires of all the bridges I've burned behind me.

      1 Reply Last reply
      0
      • T Tusen183

        OS:ubuntu22
        QT6
        I've tried various codes, but my mainWindow is always in the top left corner of my screen.

        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on last edited by JonB
        #3

        @Tusen183
        If you are running desktop under Wayland move etc,. will not work --- you cannot place the window, for now at least I don't know whether that can change in the future. You have to use Xorg in order to position the window on the desktop. See e.g. https://forum.qt.io/topic/142827/what-can-i-do-for-my-qtwidget-program-s-window-correctly-positioned-on-the-screen. Is this your situation?

        1 Reply Last reply
        0
        • S Offline
          S Offline
          SimonSchroeder
          wrote on last edited by
          #4

          Well, there is one annoying thing that might prevent this: It is not possible to center the widget on the screen from within the constructor directly. Within the constructor the widget does not have a size, yet. Qt only gives proper values for the size after showing the widget. Only then will the layout geometry be computed. Calling show itself also does not show the widget immediately because several calls to QWidget::update() are collected into a single repaint. What we do (on Windows) is to queue another slot call into the event loop which centers the widget. The order of the show event and of our center-slot seem to work out.

          This is something to consider apart from other problems you might have. In the same vein I am not sure if a move call does anything when done before the widget is actually shown on the screen.

          PS: One thought to solve the ordering problem: connect a slot to the showEvent of the widget to center it on the first show. Maybe this will work.

          1 Reply Last reply
          0

          • Login

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