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 set MainWindow at the center of any desktop ?
Forum Updated to NodeBB v4.3 + New Features

How to set MainWindow at the center of any desktop ?

Scheduled Pinned Locked Moved Solved General and Desktop
9 Posts 4 Posters 1.1k 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.
  • ApprenticeRenoA Offline
    ApprenticeRenoA Offline
    ApprenticeReno
    wrote on last edited by
    #1

    Hi guys, i tried to implement the QDesktopWidget but seems obsolete.
    There is a method that can take the mainWindow and place it in the middle of the desktop regardless of the window size? If not how can i achieve it ?

    here my main.cpp

    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
        MainWindow w;
        w.setMinimumSize(1360,710);
        w.setMaximumSize(1360,710);
        //w.centerInDesktop(?) something like this (?) 
        w.show();
        return a.exec();
    }
    
    1 Reply Last reply
    0
    • ApprenticeRenoA ApprenticeReno

      @JonB Hi, thank you for your answer. I Tried like this:

          int x,y;
          x=w.screen()->availableSize().width()/7;
          y=w.screen()->availableSize().height()/6;
          w.move(x,y);
      

      It works , but it is not /2.
      I mean, if i try it with the /2 it shows the window in between my two monitor, like, my bottom right of the first monitor and the bottom left of the second monitor.
      (A little bit more in the first monitor though) anyway;
      I then tried to "adjust" it changing the number until it shows me what i want(?), but it is not the right way to do it. isn't it?
      I mean if i deploy my app and run it in a different monitor it would appear in the wrong position so i guess i shouldn't use the /7 & /6.

      @JonB "so that you can first subtract your window's size from" My MainWindow in the ui file is 1920x1080, but then i set it to 1360x710. There is a difference between my screen size and window size?

      I tried like this:

          QScreen screen;
          QSize newDimensions;
          newDimensions=w.screen()->size()-w.screen()->availableSize()/2;
          w.move(newDimensions);
      

      So "logically speaking" should it be:
      new screen size ((1920x1080) - (1360x710))/2 ;
      w.move(new screen size);
      Is this correct?
      @JonB Thanks for your patient.

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

      @ApprenticeReno

      It works , but it is not /2.

      I'm sorry, you'll have to do your own math, and think about it logically.

      If, say, I have a screen 800 wide, and a window 200 wide, and I want it centred, I will go 800 - 200 == 600, 600 / 2 == 300, so I will place it at 300 from the left, it will end at 500, and sure enough that is also 300 from the right, so right in the middle. Draw on a piece of paper if it helps.

      I don't know about your multiple monitors, I don't use them! You will have to adjust for that.

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

        You can get current screen from your window w.screen(), then use availableSize to get screen dimensions, then you can call w.resize(newDimensions) to put the window in the centre.

        The user will still be able to move and resize the window, though.

        (Z(:^

        ApprenticeRenoA 1 Reply Last reply
        2
        • sierdzioS sierdzio

          You can get current screen from your window w.screen(), then use availableSize to get screen dimensions, then you can call w.resize(newDimensions) to put the window in the centre.

          The user will still be able to move and resize the window, though.

          ApprenticeRenoA Offline
          ApprenticeRenoA Offline
          ApprenticeReno
          wrote on last edited by
          #3

          @sierdzio "You can get current screen from your window w.screen(), then use availableSize to get screen dimensions, then you can call w.resize(newDimensions) to put the window in the centre".
          Hi, thank you for the answer, so i tried like this but it doesn't work :

              QSize newDimensions;
              newDimensions=w.screen()->availableSize();
              w.resize(newDimensions);
          

          But like this i'm just getting and then setting the same dimensions of the window.
          It doesn't change the position of the window.

          @sierdzio "The user will still be able to move and resize the window, though."

          How can the window be resized when i have set the same minimum & maximum size ?
          And why i have to resize the mainWindow? i don't want to resize it, i have already set the size that i want with this:

          w.setMinimumSize(1360,710);
          w.setMaximumSize(1360,710);
          

          What am i doing wrong ?

          @sierdzio Thanks for your patient.

          JonBJ 1 Reply Last reply
          0
          • ApprenticeRenoA ApprenticeReno

            @sierdzio "You can get current screen from your window w.screen(), then use availableSize to get screen dimensions, then you can call w.resize(newDimensions) to put the window in the centre".
            Hi, thank you for the answer, so i tried like this but it doesn't work :

                QSize newDimensions;
                newDimensions=w.screen()->availableSize();
                w.resize(newDimensions);
            

            But like this i'm just getting and then setting the same dimensions of the window.
            It doesn't change the position of the window.

            @sierdzio "The user will still be able to move and resize the window, though."

            How can the window be resized when i have set the same minimum & maximum size ?
            And why i have to resize the mainWindow? i don't want to resize it, i have already set the size that i want with this:

            w.setMinimumSize(1360,710);
            w.setMaximumSize(1360,710);
            

            What am i doing wrong ?

            @sierdzio Thanks for your patient.

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

            @ApprenticeReno
            To move where a window is you can use setGeometry() or just move(). What you need the screen() for is to get the size so that you can first subtract your window's size from and then divide x & y by 2 to achieve the centering.

            ApprenticeRenoA 1 Reply Last reply
            1
            • JonBJ JonB

              @ApprenticeReno
              To move where a window is you can use setGeometry() or just move(). What you need the screen() for is to get the size so that you can first subtract your window's size from and then divide x & y by 2 to achieve the centering.

              ApprenticeRenoA Offline
              ApprenticeRenoA Offline
              ApprenticeReno
              wrote on last edited by
              #5

              @JonB Hi, thank you for your answer. I Tried like this:

                  int x,y;
                  x=w.screen()->availableSize().width()/7;
                  y=w.screen()->availableSize().height()/6;
                  w.move(x,y);
              

              It works , but it is not /2.
              I mean, if i try it with the /2 it shows the window in between my two monitor, like, my bottom right of the first monitor and the bottom left of the second monitor.
              (A little bit more in the first monitor though) anyway;
              I then tried to "adjust" it changing the number until it shows me what i want(?), but it is not the right way to do it. isn't it?
              I mean if i deploy my app and run it in a different monitor it would appear in the wrong position so i guess i shouldn't use the /7 & /6.

              @JonB "so that you can first subtract your window's size from" My MainWindow in the ui file is 1920x1080, but then i set it to 1360x710. There is a difference between my screen size and window size?

              I tried like this:

                  QScreen screen;
                  QSize newDimensions;
                  newDimensions=w.screen()->size()-w.screen()->availableSize()/2;
                  w.move(newDimensions);
              

              So "logically speaking" should it be:
              new screen size ((1920x1080) - (1360x710))/2 ;
              w.move(new screen size);
              Is this correct?
              @JonB Thanks for your patient.

              JonBJ M 2 Replies Last reply
              0
              • ApprenticeRenoA ApprenticeReno

                @JonB Hi, thank you for your answer. I Tried like this:

                    int x,y;
                    x=w.screen()->availableSize().width()/7;
                    y=w.screen()->availableSize().height()/6;
                    w.move(x,y);
                

                It works , but it is not /2.
                I mean, if i try it with the /2 it shows the window in between my two monitor, like, my bottom right of the first monitor and the bottom left of the second monitor.
                (A little bit more in the first monitor though) anyway;
                I then tried to "adjust" it changing the number until it shows me what i want(?), but it is not the right way to do it. isn't it?
                I mean if i deploy my app and run it in a different monitor it would appear in the wrong position so i guess i shouldn't use the /7 & /6.

                @JonB "so that you can first subtract your window's size from" My MainWindow in the ui file is 1920x1080, but then i set it to 1360x710. There is a difference between my screen size and window size?

                I tried like this:

                    QScreen screen;
                    QSize newDimensions;
                    newDimensions=w.screen()->size()-w.screen()->availableSize()/2;
                    w.move(newDimensions);
                

                So "logically speaking" should it be:
                new screen size ((1920x1080) - (1360x710))/2 ;
                w.move(new screen size);
                Is this correct?
                @JonB Thanks for your patient.

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

                @ApprenticeReno

                It works , but it is not /2.

                I'm sorry, you'll have to do your own math, and think about it logically.

                If, say, I have a screen 800 wide, and a window 200 wide, and I want it centred, I will go 800 - 200 == 600, 600 / 2 == 300, so I will place it at 300 from the left, it will end at 500, and sure enough that is also 300 from the right, so right in the middle. Draw on a piece of paper if it helps.

                I don't know about your multiple monitors, I don't use them! You will have to adjust for that.

                ApprenticeRenoA 1 Reply Last reply
                2
                • ApprenticeRenoA ApprenticeReno

                  @JonB Hi, thank you for your answer. I Tried like this:

                      int x,y;
                      x=w.screen()->availableSize().width()/7;
                      y=w.screen()->availableSize().height()/6;
                      w.move(x,y);
                  

                  It works , but it is not /2.
                  I mean, if i try it with the /2 it shows the window in between my two monitor, like, my bottom right of the first monitor and the bottom left of the second monitor.
                  (A little bit more in the first monitor though) anyway;
                  I then tried to "adjust" it changing the number until it shows me what i want(?), but it is not the right way to do it. isn't it?
                  I mean if i deploy my app and run it in a different monitor it would appear in the wrong position so i guess i shouldn't use the /7 & /6.

                  @JonB "so that you can first subtract your window's size from" My MainWindow in the ui file is 1920x1080, but then i set it to 1360x710. There is a difference between my screen size and window size?

                  I tried like this:

                      QScreen screen;
                      QSize newDimensions;
                      newDimensions=w.screen()->size()-w.screen()->availableSize()/2;
                      w.move(newDimensions);
                  

                  So "logically speaking" should it be:
                  new screen size ((1920x1080) - (1360x710))/2 ;
                  w.move(new screen size);
                  Is this correct?
                  @JonB Thanks for your patient.

                  M Offline
                  M Offline
                  mpergand
                  wrote on last edited by mpergand
                  #7

                  @ApprenticeReno

                  QPoint center=w.windowHandle()->screen()->availableGeometry().center();
                  w.move(center-QPoint(w.size().width()/2,w.size().height()/2));
                  
                  ApprenticeRenoA 1 Reply Last reply
                  2
                  • JonBJ JonB

                    @ApprenticeReno

                    It works , but it is not /2.

                    I'm sorry, you'll have to do your own math, and think about it logically.

                    If, say, I have a screen 800 wide, and a window 200 wide, and I want it centred, I will go 800 - 200 == 600, 600 / 2 == 300, so I will place it at 300 from the left, it will end at 500, and sure enough that is also 300 from the right, so right in the middle. Draw on a piece of paper if it helps.

                    I don't know about your multiple monitors, I don't use them! You will have to adjust for that.

                    ApprenticeRenoA Offline
                    ApprenticeRenoA Offline
                    ApprenticeReno
                    wrote on last edited by
                    #8

                    @JonB Now i finally got it, thank you very much.

                    1 Reply Last reply
                    0
                    • M mpergand

                      @ApprenticeReno

                      QPoint center=w.windowHandle()->screen()->availableGeometry().center();
                      w.move(center-QPoint(w.size().width()/2,w.size().height()/2));
                      
                      ApprenticeRenoA Offline
                      ApprenticeRenoA Offline
                      ApprenticeReno
                      wrote on last edited by
                      #9

                      @mpergand Hi, thank you for your answer.
                      Because of @JonB i can understand the code that @mpergand posted.
                      Thank you all.

                      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