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. [Solved] QMainWindow to the center of the screen
Qt 6.11 is out! See what's new in the release blog

[Solved] QMainWindow to the center of the screen

Scheduled Pinned Locked Moved General and Desktop
10 Posts 5 Posters 21.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.
  • T Offline
    T Offline
    thehilmisu
    wrote on last edited by
    #1

    Hi everyone,
    i have a simple widget project and i want my mainwindow to be appear on the center of the user's screen.
    is this possible ?

    Thanks in advance

    1 Reply Last reply
    0
    • sierdzioS Offline
      sierdzioS Offline
      sierdzio
      Moderators
      wrote on last edited by
      #2

      Yes, it is possible. Not by using a single magic function, but pretty simple nonetheless. You need to get the available geometry ("qApp->desktop()":http://qt-project.org/doc/qt-4.8/qapplication.html#desktop) then use availableGeometry() from resulting QDesktopWidget. It returns the part of the screen that the app can be displayed on. Then it's just a bit of math: find the central point of the screen and your application's window, and align those. Just remember that in widgets, x() and y() correspond to coords including the window frame.

      (Z(:^

      1 Reply Last reply
      0
      • T Offline
        T Offline
        thehilmisu
        wrote on last edited by
        #3

        [quote author="sierdzio" date="1350891105"]Yes, it is possible. Not by using a single magic function, but pretty simple nonetheless. You need to get the available geometry ("qApp->desktop()":http://qt-project.org/doc/qt-4.8/qapplication.html#desktop) then use availableGeometry() from resulting QDesktopWidget. It returns the part of the screen that the app can be displayed on. Then it's just a bit of math: find the central point of the screen and your application's window, and align those. Just remember that in widgets, x() and y() correspond to coords including the window frame.[/quote]

        Thank you ! i did it with the code below

        @
        MyClass w;
        int width = w.frameGeometry().width();
        int height = w.frameGeometry().height();

        QDesktopWidget wid;
        
        int screenWidth = wid.screen()->width();
        int screenHeight = wid.screen()->height();
        
        w.setGeometry((screenWidth/2)-(width/2),(screenHeight/2)-(height/2),width,height);
        
        w.show();
        

        @

        1 Reply Last reply
        1
        • sierdzioS Offline
          sierdzioS Offline
          sierdzio
          Moderators
          wrote on last edited by
          #4

          Perfect! I'm pleased to read this. If you think it's solved, please add "[Solved]" to the beginning of the post's title.

          (Z(:^

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

            It can also be done like this,

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

            int W = 250;
            int H = 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 - W) / 2;
            y = (screenHeight - H) / 2;

            window.resize(W, H);
            window.move( x, y );
            window.setWindowTitle("Center");
            window.show();

            return app.exec();
            }@

            1 Reply Last reply
            0
            • X Offline
              X Offline
              XGuy
              wrote on last edited by
              #6

              simply call:

              void QWidget::showNormal () [slot]
              @this->showNoraml()@

              1 Reply Last reply
              0
              • raven-worxR Offline
                raven-worxR Offline
                raven-worx
                Moderators
                wrote on last edited by
                #7

                [quote author="XGuy" date="1376914979"]simply call:

                void QWidget::showNormal () [slot]
                @this->showNoraml()@
                [/quote]

                did you read the docs on this method?!
                Calling this method has not the effect the thread author asked for.

                --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
                If you have a question please use the forum so others can benefit from the solution in the future

                1 Reply Last reply
                0
                • X Offline
                  X Offline
                  XGuy
                  wrote on last edited by
                  #8

                  [quote author="raven-worx" date="1376919121"][quote author="XGuy" date="1376914979"]simply call:

                  void QWidget::showNormal () [slot]
                  @this->showNoraml()@
                  [/quote]

                  did you read the docs on this method?!
                  Calling this method has not the effect the thread author asked for.
                  [/quote]

                  yes you are right. sorry for that! but in my case this function maximizes my application and shows it on the center of the screen.
                  although i'm sure this approach would not work on multi-screen PCs.

                  1 Reply Last reply
                  0
                  • raven-worxR Offline
                    raven-worxR Offline
                    raven-worx
                    Moderators
                    wrote on last edited by
                    #9

                    [quote author="XGuy" date="1376971298"]but in my case this function maximizes my application and shows it on the center of the screen.[/quote]
                    maybe because it was centered before you maximized it.

                    --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
                    If you have a question please use the forum so others can benefit from the solution in the future

                    1 Reply Last reply
                    0
                    • X Offline
                      X Offline
                      XGuy
                      wrote on last edited by
                      #10

                      maybe! thanks for your advices. should i remove my wrong answer?

                      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