6

I am developing an application; which will run on a system with 2 displays. I want my PyQt app to be able to automatically route a particular window to the second screen.

How can this be done in Qt? (either in Python or C++)

3 Answers 3

4

The following works for me in PyQt5

import sys
from PyQt5.QtWidgets import QApplication, QDesktopWidget

app = QApplication(sys.argv)

widget = ... # define your widget
display_monitor = ... # the number of the monitor you want to display your widget

monitor = QDesktopWidget().screenGeometry(display_monitor)
widget.move(monitor.left(), monitor.top())
widget.showFullScreen()

Update. In PyQt6 one should use:

...
from PyQt6.QtGui import QScreen
...
monitors = QScreen.virtualSiblings(widget.screen())
monitor = monitors[display_monitor].availableGeometry()
...

Monitors should be counted starting from 0.

Sign up to request clarification or add additional context in comments.

1 Comment

Comment to the update: QDesktopWidget is deprecated in PyQt6 and one gets more readable sources, if monitor = ... line is replaced with two given lines.
3

Use QDesktopWidget to access to screen information on multi-head systems.

Here is pseudo code to make a widget cover first screen.

QDesktopWidget *pDesktop = QApplication::desktop ();

//Get 1st screen's geometry
QRect RectScreen0 = pDesktop->screenGeometry (0);

//Move the widget to first screen without changing its geometry
my_Widget->move (RectScreen0.left(),RectScreen0.top());

my_pWidget->resize (RectScreen0.width(),RectScreen0.height());

my_Widget->showMaximized();

2 Comments

is there equivalent in python?
Similar to @RémiBaudoux response, I am looking for an example with Python
1

Too late to add but it might be useful for PySide6 users coming to this question. Should work on PyQt.

Old answers hinted solution for Python. QDesktopWidget was already deprecated in Qt5 and was removed in Qt6. QScreen provides equivalent functionality for information about available screens, screen geometries. QRect property of screen geometry can be used to move widget. (https://doc.qt.io/qtforpython-6/overviews/widgets-changes-qt6.html#qdesktopwidget-and-qapplication-desktop).

app = QApplication(sys.argv)
widget = MyWidget()

screens = app.screens()
# one way; with two screens    
if len(screens) > 1:
    screen = screens[1]
else:
    screen = screens[0]

# Another way to remove primary screen and choose from remaining screens
# current_screen = app.primaryScreen()
# screens.remove(current_screen)
# screen = screens[0]

qr = screen.geometry()
widget.move(qr.left(), qr.top())
widget.show()

Cpp:

QApplication a(argc, argv);
MainWindow w;

auto screens = a.screens();
auto screen = a.screens().at(0);
if (screens.size() > 1)
    screen = screens.at(1);

w.move(screen->geometry().left(), screen->geometry().top());
w.show();
return a.exec();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.