How to move the window to the center of the screen?



  • @//Moving the window to the center of screen!
    this->move(QApplication::desktop()->availableGeometry().center() - this->rect().center());@

    Tell me if there is any other specific way to do it, something like movetocenter

    P.s shouldn't something like this be by default on a project? I mean if u just launch it it will not be at the center but e.g at top left, which is quite ugly.. 90% of the users will move the window..(i think)



  • AFAIK, there are no available methods in Qt to move a window to the center other than QDesktopWidget;



  • this work fine .....
    just new Qt simple Widget based class and place the flowing code:

    @#include <QtGui/QApplication>
    #include "widget.h"
    #include <QDesktopWidget>

    int main(int argc, char *argv[])
    {
    QApplication a(argc, argv);
    Widget widget;
    widget.adjustSize();
    widget.move(QApplication::desktop()->screen()->rect().center() - widget.rect().center());
    widget.show();

    return a.exec(&#41;;
    

    }
    @

    best wishes



  • Hope this helps you...

    @#include <QApplication>
    #include <QDesktopWidget>
    #include <QWidget>

    int main(int argc, char *argv[])
    {

    int WIDTH = 250;
    int HEIGHT = 250;

    int screenWidth;
    int screenHeight;

    int x, y;

    QApplication app(argc, argv);

    QWidget window;

    QDesktopWidget *desktop = QApplication::desktop();

    screenWidth = desktop->width();
    screenHeight = desktop->height();

    x = (screenWidth - WIDTH) / 2;
    y = (screenHeight - HEIGHT) / 2;

    window.resize(WIDTH, HEIGHT);
    window.move( x, y );
    window.setWindowTitle("Center");
    window.show();

    return app.exec();
    }@

    Thanks & Regards...



  • Riz,

    I hope you just added the link of the "zetcode":http://zetcode.com/gui/qt4/firstprograms/ site here. That code is purely similar to what is on the site (I often visit that site and I know a lot about their contents)...



  • Yep... I had added the code from "zetcode":http://zetcode.com/gui/qt4/firstprograms/ only....

    [quote author="Code_ReaQtor" date="1354790414"]Riz,

    I hope you just added the link of the "zetcode":http://zetcode.com/gui/qt4/firstprograms/ site here. That code is purely similar to what is on the site (I often visit that site and I know a lot about their contents)...[/quote]

    That's Cool, if you visit that site very often & you a know a lot about their contents... You can learn a lot from "zetcode":http://zetcode.com/gui/qt4/firstprograms/

    Thanks & Regards



  • Yeah. I think zetcode has the most recent and, AFAIK, complete and comprehensive tutorials on multiple programming languages.

    Beginners should visit the site more often. Cheers and more powers!



  • [quote author="Code_ReaQtor" date="1355644169"] Beginners should visit the site more often. ![/quote]

    Yeah.... Beginners should make use of it.. :-)



  • Addition to an old topic:

    #include <QStyle>
    #include <QDesktopWidget>
    
    window->setGeometry(
        QStyle::alignedRect(
            Qt::LeftToRight,
            Qt::AlignCenter,
            window->size(),
            qApp->desktop()->availableGeometry()
        )
    );
    

    Source : https://wiki.qt.io/How_to_Center_a_Window_on_the_Screen


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.