Results 1 to 2 of 2

Thread: Center dialog on screen problems

  1. #1
    Join Date
    Oct 2009
    Location
    Craiova, Romania
    Posts
    46
    Thanks
    3
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Center dialog on screen problems

    Hello. I am using this code to center a dialog on the screen:

    Qt Code:
    1. void centerWidgetOnScreen(QWidget* widget)
    2. {
    3. QRect rect = QApplication::desktop()->availableGeometry(widget);
    4.  
    5. widget->move(rect.center() - widget->rect().center());
    6. }
    To copy to clipboard, switch view to plain text mode 

    (where widget is in fact a QDialog*).

    But the dialog appears a bit to the right (maybe 10 pixels) and too low (maybe 30 pixels, considering also the task bar).

    What am I doing wrong?

    I also tried this:

    Qt Code:
    1. QRect frect=widget->frameGeometry();
    2. frect.moveCenter(QApplication::desktop()->availableGeometry(widget).center());
    3. //frect.moveCenter(QDesktopWidget().availableGeometry(widget).center());
    4. widget->move(frect.topLeft());
    To copy to clipboard, switch view to plain text mode 

    But does not work under Linux. By the way, which should I use, QApplication::desktop() or QDesktopWidget()?
    Last edited by lalesculiviu; 1st November 2009 at 09:34.

  2. #2
    Join Date
    Sep 2009
    Location
    Nanjing, China
    Posts
    46
    Thanks
    12
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Center dialog on screen problems

    If it is a subclass of QDialog, why not use the parent parameter of QDialog's constractor? If the parent is a QWidget, then this dialog will center at this parent widget, if is null, this dialog will be at the center of screen.

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. 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
  3. Show dialog in screen center
    By mourad in forum Qt Programming
    Replies: 1
    Last Post: 16th June 2008, 13:41
  4. how to show Dialog as drawer on the bottom of the parent screen?
    By vishal.chauhan in forum Qt Programming
    Replies: 1
    Last Post: 7th March 2008, 08:00
  5. Dialog sizes and different screen resolutions.
    By hvengel in forum Qt Tools
    Replies: 3
    Last Post: 2nd April 2006, 10:05

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.