3

I have a Windows-system with two monitors connected to it that itself expand the Windows-desktop. Now I want to start two Qt-applications but need to force each of them to a specific monitor, means application A always has to open it's window on monitor 1, application B always has to open it's window on monitor 2 (no matter where they have been opened the last time and no matter where the mouse is located at the moment).

How can this be done automatically? Can it only be done via the screen-coordinates of the desktop? If yes: how can I force my QWidget-based window to a specific coordinate? If no: how else can this be done?

1 Answer 1

8

To get the number of screens at runtime you can use:

int screenCount = QApplication::desktop()->screenCount(); 

To get the geometry of a screen, you can use:

QRect screenRect = QApplication::desktop()->screenGeometry(1); // 0-indexed, so this would get the second screen 

Moving a window to that position (or resizing it) is then trivial:

yourWindow->move(QPoint(screenRect.x(), screenRect.y())); 
Sign up to request clarification or add additional context in comments.

1 Comment

ATTENTION in HighDPI: this can have very weird side-effects (e.g. window jumps to yet another screen) if your screens have different DPI (or rather DPR, the device pixel ratio) because QWidget::move() works in the DPR of the currently owning screen. You need to multiply your QPoint by yourWindow's DPR and divide by the target screen's DPR: yourWindow->move(QPoint(screenRect.x(), screenRect.y()) * (yourWindow->devicePixelRatio() / QApplication::screens()[1]->devicePixelRatio()));

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.