QML - main window position on start (screen center)



  • Hello all,

    How I can do following: I'd like show my main window on start on the center screen.



  • Hello AntyaDev,
    If you are using C++ also then you can put these line in main.cpp file.

    @void setCenterOfApplication(QWidget* widget)
    {
    QSize size = widget->sizeHint();
    QDesktopWidget* desktop = QApplication::desktop();
    int width = desktop->width();
    int height = desktop->height();
    int mw = size.width();
    int mh = size.height();
    int centerW = (width/2) - (mw/2);
    int centerH = (height/2) - (mh/2);
    widget->move(centerW, centerH);
    }@



  • this is another one
    put these line in main.cpp file

    @void center(QWidget &widget ,int WIDTH , int HEIGHT)
    {
    int x, y;
    int screenWidth;
    int screenHeight;

    QDesktopWidget *desktop = QApplication::desktop();

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

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

    widget.setGeometry(x, y, WIDTH, HEIGHT);

    }@



  • this option is a more efficient!

    @
    void YourClass::centerWidget(QWidget *w, bool useSizeHint)
    {
    if(w->isFullScreen())
    return;

    QSize size;
    if(useSizeHint)
        size = w->sizeHint();
    else
        size = w->size();
    
    QDesktopWidget *d = QApplication::desktop();
    int ws = d->width();   // returns screen width
    int h = d->height();  // returns screen height
    int mw = size.width();
    int mh = size.height();
    int cw = (ws/2) - (mw/2);
    int ch = (h/2) - (mh/2);
    w->move(cw,ch);
    

    }
    @


Log in to reply
 

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