+ Reply to Thread
Results 1 to 5 of 5

Thread: How to center main window on screen?

  1. #1
    Join Date
    Jul 2011
    Location
    Nevada, USA
    Posts
    5
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Question How to center main window on screen?

    I've looked high and low for this answer, so I'm not just throwing out a stupid question. I cannot find any information to center my main application window for the desktop. Just a standard program made in Designer, a mainform, and in the properties there is no such option like there is in VS-2005. I've also done searches here, and in the docs through Creator.

    I have the code I've saved for years that works in standard C++, but it won't work in Qt. The handles are much different.

    So, what's the secret?

    PS: I saw the post by lovelypp already. It didn't work for me.

  2. #2
    Join Date
    Jul 2010
    Location
    Indonesia
    Posts
    83
    Thanked 17 Times in 17 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Maemo/MeeGo

    Default Re: How to center main window on screen?

    Simply move the window to center then show it. This can be done by calling QWidget::show() then QWidget::move(int, int) or QWidget::move(const QPoint&). You can access information about current desktop screen geometry from QApplication::desktop()->screenGeometry().
    The implementation should be like this.
    Qt Code:
    1. QRect screenGeometry = QApplication::desktop()->screenGeometry();
    2. int x = (screenGeometry.width()-mainWindow->width()) / 2;
    3. int y = (screenGeometry.height()-mainWindow->height()) / 2;
    4. mainWindow->move(x, y);
    5. mainWindow->show();
    To copy to clipboard, switch view to plain text mode 
    ~ We are nothing in this universe ~

  3. The following user says thank you to viulskiez for this useful post:

    LarryP (16th July 2011)

  4. #3
    Join Date
    Jul 2011
    Location
    Nevada, USA
    Posts
    5
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to center main window on screen?

    Quote Originally Posted by viulskiez View Post
    Simply move the window to center then show it. This can be done by calling QWidget::show() then QWidget::move(int, int) or QWidget::move(const QPoint&). You can access information about current desktop screen geometry from QApplication::desktop()->screenGeometry().
    The implementation should be like this.
    Qt Code:
    1. QRect screenGeometry = QApplication::desktop()->screenGeometry();
    2. int x = (screenGeometry.width()-mainWindow->width()) / 2;
    3. int y = (screenGeometry.height()-mainWindow->height()) / 2;
    4. mainWindow->move(x, y);
    5. mainWindow->show();
    To copy to clipboard, switch view to plain text mode 
    My man, you got it! Oh how I thank you so much! I had to use "this" in the place of my MainWindow object which is not a good habit I know, but it works. I really thank you!

  5. #4
    Join Date
    Oct 2016
    Location
    Duckburg, Calisota
    Posts
    2
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: How to center main window on screen?

    This worked for me:

    Qt Code:
    1. mainWindow->setGeometry(QStyle::alignedRect(Qt::LeftToRight, Qt::AlignCenter, mainWindow->size(), qApp->desktop()->availableGeometry()));
    To copy to clipboard, switch view to plain text mode 

  6. #5
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    3,650
    Thanks
    211
    Thanked 560 Times in 550 Posts
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows Android

    Default Re: How to center main window on screen?

    Makes my head hurt to read code with so many pieces of functionality crammed into a single line.

    The only significant difference between your code and the previous, nearly 8 year old post is the use of "availableGeometry" rather than "screenGeometry" because it takes into account window system decorations that subtract from the usable screen area. This makes a small, but probably better result.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

+ Reply to Thread

Similar Threads

  1. how to show window in the center of the screen?
    By lovelypp in forum Qt Programming
    Replies: 5
    Last Post: 18th March 2014, 21:43
  2. Center dialog on screen problems
    By lalesculiviu in forum Qt Programming
    Replies: 1
    Last Post: 1st November 2009, 13:15
  3. how to put a dialog in the center of the screen
    By biswajithit in forum Qt Programming
    Replies: 4
    Last Post: 4th September 2008, 12:24
  4. Replies: 11
    Last Post: 11th August 2008, 09:14
  5. Show dialog in screen center
    By mourad in forum Qt Programming
    Replies: 1
    Last Post: 16th June 2008, 13:41

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts