TFM
TFM

Reputation: 717

QML Force window to be shown on first screen

I have the following QML component:

import QtQuick 2.3
import QtQuick.Window 2.3

Window
{
  id: main
  visible: true
  width: 500
  height: 500

  screen: Qt.application.screens[0]

  Text
  {
    text: "Hello World!"
  }
}

used the following way:

#include <QApplication>
#include <QQmlApplicationEngine>
#include <QString>

int main(int argc, char** argv)
{
    QApplication app{argc, argv};
    QQmlApplicationEngine engine;
    engine.load(QUrl{QStringLiteral("qrc:/main.qml")});
    return app.exec();
}

I'm running this on a system with two screens.

As long as I have screen: Qt.application.screens[0], the window will always be shown on the screen where the mouse is when the app is started.

If I change it to screen: Qt.application.screens[1], the window will always be shown on the second screen, regardless of current mouse position.

Qt.application.screens[2] does not seem to exist.

Now two questions: Is this behavior of screen: Qt.application.screens[0] expected, i.e. is the first element in that array always the current screen?

And if so, how do I actually get the first screen?

Have tried this with Qt versions 5.9.6 and 5.12.3.

Upvotes: 2

Views: 2552

Answers (2)

p-a-o-l-o
p-a-o-l-o

Reputation: 10047

On a two-screens virtual desktop, I would try to set the window coordinates, right after specifying the screen. The following code is supposed to center the window in the first screen of the Qt.application.screens array:

  screen: Qt.application.screens[0]
  x: screen.virtualX + ((screen.width - width) / 2)
  y: screen.virtualY + ((screen.height - height) / 2)

Notice I used virtualX and virtualY properties, i.e. the coordinates of the screen within the virtual desktop. This way the window will stay centered to whatever screen it belongs.

By the way, one could omit the screen property altogether and set coordinates only:

  x: 0
  y: 0

The above code will position the window to the top-left corner of the left screen, i.e. the absolute top-left corner of the virtual desktop.

Upvotes: 1

Vaidotas Strazdas
Vaidotas Strazdas

Reputation: 716

If I understand the question correctly, Qt.application.screens[0] means just accessing the first element of the following array: https://doc.qt.io/qt-5/qguiapplication.html#screens . Order of QScreen instances of this method is not described in the documentation, but it looks like that 0th index means the first screen. However, there is also another method to get the first screen. However, keep in mind that saying "the first screen" is not quite correct naming. You can physically swap your screens, and then which screen will be the first one? You should understand that Qt can not know if you have swapped your screens physically or not. Thus, there is more correct call for this type of screen. The screen you would probably treat as the first one is called the primary screen by Qt. The method to access that screen is the following: https://doc.qt.io/qt-5/qguiapplication.html#primaryScreen-prop

In other words, your needs might be satisfied by the following approach: Instead of screen: Qt.application.screens[0], you should use screen: Qt.application.primaryScreen. it might be possible that the first approach will also work, but usage of primaryScreen looks much more declarative and clear than the first approach.

Upvotes: 0

Related Questions