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 move the window to the center of the screen?
Qt 6.11 is out! See what's new in the release blog

How to move the window to the center of the screen?

Scheduled Pinned Locked Moved General and Desktop
10 Posts 6 Posters 59.1k Views 1 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.
  • L Offline
    L Offline
    Leon
    wrote on last edited by
    #1

    @//Moving the window to the center of screen!
    this->move(QApplication::desktop()->availableGeometry().center() - this->rect().center());@

    Tell me if there is any other specific way to do it, something like movetocenter

    P.s shouldn't something like this be by default on a project? I mean if u just launch it it will not be at the center but e.g at top left, which is quite ugly.. 90% of the users will move the window..(i think)

    1 Reply Last reply
    0
    • C Offline
      C Offline
      Code_ReaQtor
      wrote on last edited by
      #2

      AFAIK, there are no available methods in Qt to move a window to the center other than QDesktopWidget;

      Please visit my open-source projects at https://github.com/Code-ReaQtor.

      1 Reply Last reply
      0
      • ? Offline
        ? Offline
        A Former User
        wrote on last edited by
        #3

        this work fine .....
        just new Qt simple Widget based class and place the flowing code:

        @#include <QtGui/QApplication>
        #include "widget.h"
        #include <QDesktopWidget>

        int main(int argc, char *argv[])
        {
        QApplication a(argc, argv);
        Widget widget;
        widget.adjustSize();
        widget.move(QApplication::desktop()->screen()->rect().center() - widget.rect().center());
        widget.show();

        return a.exec(&#41;;
        

        }
        @

        best wishes

        1 Reply Last reply
        1
        • M Offline
          M Offline
          Macro
          wrote on last edited by
          #4

          Hope this helps you...

          @#include <QApplication>
          #include <QDesktopWidget>
          #include <QWidget>

          int main(int argc, char *argv[])
          {

          int WIDTH = 250;
          int HEIGHT = 250;

          int screenWidth;
          int screenHeight;

          int x, y;

          QApplication app(argc, argv);

          QWidget window;

          QDesktopWidget *desktop = QApplication::desktop();

          screenWidth = desktop->width();
          screenHeight = desktop->height();

          x = (screenWidth - WIDTH) / 2;
          y = (screenHeight - HEIGHT) / 2;

          window.resize(WIDTH, HEIGHT);
          window.move( x, y );
          window.setWindowTitle("Center");
          window.show();

          return app.exec();
          }@

          Thanks & Regards...

          1 Reply Last reply
          0
          • C Offline
            C Offline
            Code_ReaQtor
            wrote on last edited by
            #5

            Riz,

            I hope you just added the link of the "zetcode":http://zetcode.com/gui/qt4/firstprograms/ site here. That code is purely similar to what is on the site (I often visit that site and I know a lot about their contents)...

            Please visit my open-source projects at https://github.com/Code-ReaQtor.

            1 Reply Last reply
            0
            • M Offline
              M Offline
              Macro
              wrote on last edited by
              #6

              Yep... I had added the code from "zetcode":http://zetcode.com/gui/qt4/firstprograms/ only....

              [quote author="Code_ReaQtor" date="1354790414"]Riz,

              I hope you just added the link of the "zetcode":http://zetcode.com/gui/qt4/firstprograms/ site here. That code is purely similar to what is on the site (I often visit that site and I know a lot about their contents)...[/quote]

              That's Cool, if you visit that site very often & you a know a lot about their contents... You can learn a lot from "zetcode":http://zetcode.com/gui/qt4/firstprograms/

              Thanks & Regards

              1 Reply Last reply
              0
              • C Offline
                C Offline
                Code_ReaQtor
                wrote on last edited by
                #7

                Yeah. I think zetcode has the most recent and, AFAIK, complete and comprehensive tutorials on multiple programming languages.

                Beginners should visit the site more often. Cheers and more powers!

                Please visit my open-source projects at https://github.com/Code-ReaQtor.

                1 Reply Last reply
                0
                • M Offline
                  M Offline
                  Macro
                  wrote on last edited by
                  #8

                  [quote author="Code_ReaQtor" date="1355644169"] Beginners should visit the site more often. ![/quote]

                  Yeah.... Beginners should make use of it.. :-)

                  1 Reply Last reply
                  0
                  • H Offline
                    H Offline
                    Harry123
                    wrote on last edited by
                    #9

                    Addition to an old topic:

                    #include <QStyle>
                    #include <QDesktopWidget>
                    
                    window->setGeometry(
                        QStyle::alignedRect(
                            Qt::LeftToRight,
                            Qt::AlignCenter,
                            window->size(),
                            qApp->desktop()->availableGeometry()
                        )
                    );
                    

                    Source : https://wiki.qt.io/How_to_Center_a_Window_on_the_Screen

                    1 Reply Last reply
                    2
                    • Jimmy CrabJ Offline
                      Jimmy CrabJ Offline
                      Jimmy Crab
                      wrote on last edited by Jimmy Crab
                      #10
                      This post is deleted!
                      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