为 PyQt6 应用程序定义监视器

Define monitor for PyQt6 application

我想知道如何明确定义应用程序启动的监视器。
申请结果:

qt.qpa.window: Window position QRect(468,-22 504x896)
outside any known screen, using primary screen

执行时。
MainWindow 对象松散定义如下:

import sys
from PyQt6.QtWidgets import QMainWindow, QApplication


class MainWindow(QMainWindow):

    def center(self):
        """
        Center window in middle of screen 2
        """

        qr = self.frameGeometry()
        cp = self.screen().availableGeometry().center()

        qr.moveCenter(cp)
        self.move(qr.topLeft())

    def __init__(self) -> None:
        super().__init__()

        self.center()
        self.show()


def main():
    app = QApplication(sys.argv)

    # Init window
    window = MainWindow()
    sys.exit(app.exec())

我在 Mac 笔记本电脑上,我有多个虚拟桌面,PyQt6 假定它们是物理监视器。

如何明确设置应用程序在主/第一显示器上打开?

你能试试下面这样的东西吗? (我没有环境。我没有尝试。)。如果这不起作用,请告诉我。我会删除这个 post.

#MAINWINDOW OBJECT
window = MainWindow()

#GET QWINDOW OBJECT
win = window.windowHandle()

#SET PRIMARY SCREEN
win.setScreen(app.primaryScreen()) #app is your application object.

文档中也有注释

https://doc.qt.io/qtforpython-5/PySide2/QtGui/QWindow.html#PySide2.QtGui.PySide2.QtGui.QWindow.setScreen

Note: If the screen is part of a virtual desktop of multiple screens, the window will not move automatically to newScreen. To place the window relative to the screen, use the screen's topLeft() position.

您也可以尝试获取所有可用屏幕

screens = app.screens()

也许你可以迭代它们并设置你想要的屏幕。

win.setScreen(screens[0]) #example