Results 1 to 6 of 6

Thread: how to show window in the center of the screen?

  1. #1

    Question how to show window in the center of the screen?

    how to center the window in the screen ? is there an centreWindow method ?
    thanks!

  2. #2
    Join Date
    Dec 2006
    Posts
    849
    Thanks
    6
    Thanked 163 Times in 151 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Red face Re: how to show window in the center of the screen?

    Check out
    QDesktopWidget::availableGeometry()QWidget::frameGeometry()QRect::moveCenter()
    so, something like
    Qt Code:
    1. // w is your window
    2. QRect r = w->frameGeometry();
    3. r.moveCenter(QDesktopWidget::availableGeometry());
    4. w->move(r.topLeft());
    To copy to clipboard, switch view to plain text mode 
    might work.

    You have to take care about when you call that, though.
    Things like frameGeometry() etc return incorrect values if called "too soon" (if the widget is not yet shown).
    You might have to execute that code in reaction to an internal QShowEvent.

    HTH

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

    giowck (7th September 2010)

  4. #3

    Default Re: how to show window in the center of the screen?

    I just want the window to be shown central the first time it is shown. so How ?
    thanks anyway!

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

    Default Re: how to show window in the center of the screen?

    In Visual Studio when you design a form, it has the option to center it a few different ways. Designer should also have that option in the properties section, but nothing even remotely related is there for window placement. However, I'm certainly not going back to VS anytime soon if ever.

  6. #5
    Join Date
    Jun 2011
    Posts
    21
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: how to show window in the center of the screen?

    use this code in constructor
    Qt Code:
    1. QDesktopWidget *desktop = QApplication::desktop();
    2.  
    3. int screenWidth, width;
    4. int screenHeight, height;
    5. int x, y;
    6. QSize windowSize;
    7.  
    8. screenWidth = desktop->width();
    9. screenHeight = desktop->height();
    10.  
    11. windowSize = size();
    12. width = windowSize.width();
    13. height = windowSize.height();
    14.  
    15.  
    16. x = (screenWidth - width) / 2;
    17. y = (screenHeight - height) / 2;
    18. y -= 50;
    19.  
    20.  
    21.  
    22. move ( x, y );
    23.  
    24. setFixedSize(windowSize.width(), windowSize.height());
    To copy to clipboard, switch view to plain text mode 

  7. #6
    Join Date
    Mar 2014
    Posts
    1
    Qt products
    Qt4

    Default Re: how to show window in the center of the screen?

    include QDesktopWidget:
    #include <QDesktopWidget>

    after that let's say your window is "w" (MainWindow w
    w.move(d.geometry().center().x() - w.width() / 2, d.geometry().center().y() - w.height() / 2);

Similar Threads

  1. Show dialog in screen center
    By mourad in forum Qt Programming
    Replies: 1
    Last Post: 16th June 2008, 13:41
  2. Set a window as child at runtime
    By sabeesh in forum Qt Programming
    Replies: 1
    Last Post: 26th November 2007, 09:30
  3. qtopia window decoration cannot show caption
    By qtopiahooo in forum Qt for Embedded and Mobile
    Replies: 1
    Last Post: 13th March 2007, 08:09
  4. window decoration cannot show caption
    By qtopiahooo in forum Qt Programming
    Replies: 1
    Last Post: 31st January 2007, 08:17
  5. WYSIWYG html, Window show png icon mac no!
    By patrik08 in forum Qt Programming
    Replies: 10
    Last Post: 25th May 2006, 12:01

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.