Results 1 to 7 of 7

Thread: set QMainWindow in the center of my desktop

  1. #1
    Join Date
    Jan 2006
    Posts
    273
    Thanks
    42
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default set QMainWindow in the center of my desktop

    Hi everybody,

    QT: 4.1.3
    OS: WinXP PRO
    Compiler: MINGW

    I am trying to set my app in the midle of my desktop. How can i do tha?Should i use setCentralWidget??

    Qt Code:
    1. #include <QApplication>
    2. #include "mainwindow.h"
    3.  
    4.  
    5. int main(int argc, char *argv[])
    6. {
    7.  
    8.  
    9. QApplication app(argc, argv);
    10. app.setStyle("plastique");
    11. app.setPalette( app.style()->standardPalette() );
    12. MainWindow m;
    13. m.setMinimumSize(451,600);
    14. m.setMaximumSize(451,600);
    15. // m.setCentralWidget();
    16.  
    17. m.show();
    18. return app.exec();
    19. }
    To copy to clipboard, switch view to plain text mode 
    Think DigitalGasoline

  2. #2
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: set QMainWindow in the center of my desktop

    QDesktopWidget provides access to screen geometries.
    J-P Nurmi

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

    raphaelf (24th August 2006)

  4. #3
    Join Date
    Jul 2006
    Location
    Slovakia
    Posts
    17
    Thanks
    12
    Thanked 6 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: set QMainWindow in the center of my desktop

    This gives you what you want (copy to constructor of your "MainWindow"):

    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(); // get width of screen
    9. screenHeight = desktop->height(); // get height of screen
    10.  
    11. windowSize = size(); // size of our application window
    12. width = windowSize.width();
    13. height = windowSize.height();
    14.  
    15. // little computations
    16. x = (screenWidth - width) / 2;
    17. y = (screenHeight - height) / 2;
    18. y -= 50;
    19.  
    20. // move window to desired coordinates
    21. move ( x, y );
    To copy to clipboard, switch view to plain text mode 
    Last edited by sector; 23rd August 2006 at 17:11.

  5. The following 2 users say thank you to sector for this useful post:

    castors33 (22nd May 2012), raphaelf (24th August 2006)

  6. #4
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: set QMainWindow in the center of my desktop

    Qt Code:
    1. QRect r = widget->geometry();
    2. r.moveCenter(QApplication::desktop()->availableGeometry().center());
    3. widget->setGeometry(r);
    To copy to clipboard, switch view to plain text mode 

    Be aware that constructor of a widget may not always be a reliable place to ask widget for it's geometries...
    J-P Nurmi

  7. The following user says thank you to jpn for this useful post:

    raphaelf (24th August 2006)

  8. #5
    Join Date
    Jan 2006
    Posts
    75
    Thanks
    3
    Thanked 5 Times in 4 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: set QMainWindow in the center of my desktop

    In the QWidget subclass object construtor, add this line:

    this->move(QApplication::desktop()->screen()->rect().center()-this->rect().center());

  9. The following 2 users say thank you to ball for this useful post:

    alamgir (18th February 2011), raphaelf (24th August 2006)

  10. #6
    Join Date
    Jan 2006
    Posts
    273
    Thanks
    42
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: set QMainWindow in the center of my desktop

    Hi everybody!!

    Now i have a lot of solutions

    It works

    Thanks to all

    Qt Code:
    1. int main(int argc, char *argv[])
    2. {
    3.  
    4.  
    5. QApplication app(argc, argv);
    6. app.setStyle("plastique");
    7. app.setPalette( app.style()->standardPalette() );
    8. MainWindow m;
    9. m.setMinimumSize(451,600);
    10. m.setMaximumSize(451,600);
    11. QRect r = m.geometry();
    12. r.moveCenter(QApplication::desktop()->availableGeometry().center());
    13. m.setGeometry(r);
    14.  
    15. m.show();
    16. return app.exec();
    17. }
    To copy to clipboard, switch view to plain text mode 
    Think DigitalGasoline

  11. #7
    Join Date
    Nov 2012
    Location
    Russia
    Posts
    231
    Thanks
    118
    Thanked 18 Times in 10 Posts
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: set QMainWindow in the center of my desktop

    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. Dynamically Loading a QMainWindow?
    By natron in forum Newbie
    Replies: 10
    Last Post: 21st July 2006, 02:15
  2. Insert separate QToolBar into QMainWindow
    By YuriyRusinov in forum Qt Programming
    Replies: 3
    Last Post: 24th April 2006, 11:37
  3. Replies: 18
    Last Post: 22nd February 2006, 21:51

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.