简体   繁体   English

macOS 上的 Qt GUI 应用程序:如何找到当前活动的屏幕?

[英]Qt GUI application on macOS: how to find the currently active screen?

We are developing a macOS application whose GUI is relying on Qt.我们正在开发一个 macOS 应用程序,其 GUI 依赖于 Qt。

At startup, we want to show() the QMainWindow at a specific location on the currently active screen (with multi screen systems in mind).在启动时,我们希望在当前活动屏幕的特定位置show() QMainWindow (考虑到多屏幕系统)。

Is there a way to get the QScreen representing the currently active screen?有没有办法让QScreen代表当前活动的屏幕?

From our test, QGuiApplication::primaryScreen() is the first screen (which is consistent with the name), but we cannot find an equivalent for the active screen.从我们的测试中, QGuiApplication::primaryScreen()是第一个屏幕(与名称一致),但我们找不到活动屏幕的等效项。

Qt5 provides functionality to do so, the QWindow::setScreen method sets the screen on which the window should be shown. Qt5 提供了这样做的功能, QWindow::setScreen方法设置应该显示窗口的屏幕。

Any widget provides access to this pointer via QWidget::windowHandle() :任何小部件都通过QWidget::windowHandle()提供对这个指针的访问:

QWidget * widget = new QWidget();
auto screens = qApp->screens();
// compute the index
widget->windowHandle()->setScreen(screens[index]);
widget->showFullScreen();

To get the screen number, you can use the mouse position and assume that the screen with the mouse is the one with the current focus:要获取屏幕编号,您可以使用鼠标位置并假设鼠标所在的屏幕是当前焦点所在的屏幕:

QPoint globalCursorPos = QCursor::pos();
int mouseScreen = qApp->desktop()->screenNumber(globalCursorPos); 

So the final code can be something like that:所以最终的代码可以是这样的:

QWidget * widget = new QWidget();
const auto globalCursorPos = QCursor::pos();
const auto mouseScreen = qApp->desktop()->screenNumber(globalCursorPos); 
widget->windowHandle()->setScreen(qApp->screens()[mouseScreen]);
widget->showFullScreen();

Windows视窗

If this approach does not fit your needs, you will need to perform some OS calls.如果此方法不符合您的需要,您将需要执行一些操作系统调用。

For instance, on Windows, you can use MonitorFromWindow :例如,在 Windows 上,您可以使用MonitorFromWindow

HMONITOR active_monitor_number = MonitorFromWindow(GetActiveWindow(), MONITOR_DEFAULTTONEAREST);

If you need more information about the screen, you can use Qt or GetMonitorInfo .如果您需要有关屏幕的更多信息,可以使用 Qt 或GetMonitorInfo

I am not a Mac OS X developer, but it may exist a similar API我不是 Mac OS X 开发人员,但它可能存在类似的 API

I did it in the following way:我是通过以下方式做到的:

#if (QT_VERSION >= QT_VERSION_CHECK(5, 10, 0))
    QPoint topLeft = QPoint(0, 0);
    QScreen* currentScreen = QGuiApplication::screenAt(QCursor::pos());
    if (nullptr != currentScreen) {
        topLeft = currentScreen->geometry().topLeft();
    }
#else
    QPoint topLeft =
      qApp->desktop()
        ->screenGeometry(qApp->desktop()->screenNumber(QCursor::pos()))
        .topLeft();
#endif
    someWidget->move(mapFromGlobal(topLeft) + QPoint(offset, offset));

Note that, sometimes, you may get nullptr for currentScreen (in my case if primary screen is at the bottom and mouse pos at the bottom or left edge on the primary screen).请注意,有时,您可能会为currentScreen获得nullptr (在我的情况下,如果主屏幕位于底部,鼠标位置位于主屏幕的底部或左边缘)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM