Results 1 to 6 of 6

Thread: How to center main window on screen?

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    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 

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,315
    Thanks
    314
    Thanked 870 Times in 857 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.

  3. #3
    Join Date
    Nov 2012
    Posts
    232
    Thanks
    118
    Thanked 18 Times in 10 Posts
    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, 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
  •  
Qt is a trademark of The Qt Company.