QML - main window position on start (screen center)

If using QtQuick, it's possible to do that:

import QtQuick 2.2
import QtQuick.Controls 1.1
import QtQuick.Window 2.0

ApplicationWindow {
    visible: true
    width: 320
    height: 480
    Component.onCompleted: {
        // Commenting this to use properties instead of setters
        //setX(Screen.width / 2 - width / 2);
        //setY(Screen.height / 2 - height / 2);
        x = Screen.width / 2 - width / 2
        y = Screen.height / 2 - height / 2
    }
}

Dielson's answer is much better, especially since widgets weren't mentioned... anyway, here's an even simpler version of his answer:

import QtQuick 2.0
import QtQuick.Window 2.0

Window {
    visible: true
    x: Screen.width / 2 - width / 2
    y: Screen.height / 2 - height / 2
    width: 320
    height: 480
}

As mentioned by Alexander, this binding can result in weird resizing behaviour. Because of that, it's better to use Dielson's answer. The only thing I'd mention is that it's not common to use setters in QML; some systems (I believe they're called property interceptors) even rely on properties being set to perform animations, for example. So the more common approach is as follows:

import QtQuick 2.0
import QtQuick.Window 2.0

Window {
    visible: true
    width: 320
    height: 480

    Component.onCompleted: {
        x = Screen.width / 2 - width / 2
        y = Screen.height / 2 - height / 2
    }
}

After examining both replies and actually debugging the code with Qt 5.9.1 it shows more than one issue with original replies:

  1. Cannot bind [x, y] to [width, height] unless we want to see strange effects with resizing.
  2. Even though [x, y] change in Component.onCompleted seems to be logical it does not work as expected with 2 monitors of different DPI (as on systems I currently develop on).
  3. Need to use Window.screen instead of Screen singleton type. That way we get actual screen matching the window on.
  4. To completely untie [x, y] from dynamic values but the actual window screen at the moment of the initial window showing we now use onScreenChanged which is a handler for the screen property change.

This solution is more complete and uses Window.screen property:

ApplicationWindow {
    id: window
    property bool screenInit: false

    title: qsTr("App Window Positioning")
    visible: true

    height: Theme.windowHeight // initial
    width: Theme.windowWidth   // initial

    Connections {
        target: window
        onScreenChanged: if (!screenInit) {
            // we have actual screen delivered here for the time when app starts
            screenInit = true
            window.x = screen.width / 2 - Theme.windowWidth / 2
            window.y = screen.height / 2 - Theme.windowHeight / 2
        }
    }
}

P.S. If so I used ApplicationWindow type which is derived from Window and it should be consistent with Window positioning behavior.


You'll need to setGeometry on your top-level widget before you show it. The easiest way I can think of to work out what geometry you need is via QDesktopWidget. Try the example below (create a QPushButton, press it while moving the widget around various screens) and you'll see what I mean:

MainWindow::MainWindow(QWidget *parent) :
  QMainWindow(parent),
  ui(new Ui::MainWindow)
{   
  ui->setupUi(this);
  connect(ui->pushButton, SIGNAL(released()), this, SLOT(ButtonPressed()));
}

MainWindow::~MainWindow()
{
  delete ui;
}

void MainWindow::ButtonPressed()
{
  qDebug() << QApplication::desktop()->screenCount();
  qDebug() << QApplication::desktop()->screenNumber();
  qDebug() << QApplication::desktop()->screenGeometry(this);
}

Should be reasonably simple from there to come up with a generic version that works out a user's center screen (if it exists).

Tags:

C++

Qt

Qml

Qt Quick