Results 1 to 3 of 3

Thread: center application on screen in multiheaded system

  1. #1

    Default center application on screen in multiheaded system

    i'm trying to center my app on whatever screen it is currently on, on a multiheaded system. i get how to center it, but the problem i'm having is i am getting 0x0 for the dimension of my program. i call resize(0,0) before center it to compact the program to it's minimum size. w.geometry().width()/height() and w.size().width()/height() both return zero. is there some other way i should be compacting the main window or some other way to get *actual* dimensions? if i skip the call to resize() i get 640x480 as the size of the window which is not accurate either.

    Qt Code:
    1. // w is a QMainWindow
    2.  
    3. // compact the program
    4. w.resize(0,0);
    5.  
    6. // center it
    7. QDesktopWidget *desktop = QApplication::desktop();
    8. int screenNumber = desktop->screenNumber(&w);
    9. int screenWidth = desktop->screenGeometry(desktop->screen(screenNumber)).width();
    10. int screenHeight = desktop->screenGeometry(desktop->screen(screenNumber)).height();
    11. int x = (screenWidth - w.geometry().width()) / 2;
    12. int y = (screenHeight - w.geometry().height()) / 2;
    13. w.move(x, y - 50);
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: center application on screen in multiheaded system

    If you are running the code above in the constructor of your widget then there is probably no meaningful size: try looking after you show() it. It is also possible that the window can resize to zero if nothing inside it constrains the minimum size.

  3. #3

    Default Re: center application on screen in multiheaded system

    yep... show() first did the trick.

Similar Threads

  1. How to center main window on screen?
    By LarryP in forum Qt Programming
    Replies: 5
    Last Post: 24th July 2020, 20:29
  2. 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
  3. Center dialog on screen problems
    By lalesculiviu in forum Qt Programming
    Replies: 1
    Last Post: 1st November 2009, 13:15
  4. 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
  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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.