Results 1 to 6 of 6

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
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    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.

  7. #6
    Join Date
    Nov 2012
    Location
    Russia
    Posts
    231
    Thanks
    118
    Thanked 18 Times in 10 Posts
    Qt products
    Qt5
    Platforms
    Windows Android

    Default Re: How to center main window on screen?

    These functions are obsolete:
    Qt Code:
    1. QApplication::desktop()->screenGeometry()
    2. QApplication::desktop()->availableGeometry()
    3. QDesktopWidget::screen()
    To copy to clipboard, switch view to plain text mode 

    Use QScreen instead:

    Qt Code:
    1. #include "MainWindow.h"
    2. #include "ui_MainWindow.h"
    3. #include <QScreen>
    4.  
    5. MainWindow::MainWindow(QWidget *parent)
    6. : QMainWindow(parent)
    7. , ui(new Ui::MainWindow)
    8. {
    9. ui->setupUi(this);
    10. move(QGuiApplication::screens().at(0)->geometry().center() - frameGeometry().center());
    11. }
    To copy to clipboard, switch view to plain text mode 

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, 22:43
  2. Center dialog on screen problems
    By lalesculiviu in forum Qt Programming
    Replies: 1
    Last Post: 1st November 2009, 14: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, 13:24
  4. Replies: 11
    Last Post: 11th August 2008, 10:14
  5. Show dialog in screen center
    By mourad in forum Qt Programming
    Replies: 1
    Last Post: 16th June 2008, 14: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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.