setGeometry 在 OS X 上使用 Qt6 设置顶部坐标失败

问题描述 投票:0回答:1

我正在创建一个名为

showLeftAligned
的方法,以强制 QDialog 的几何图形在屏幕上左对齐,并由周围
gap
像素的间隙分隔开。

void QXDialog::showLeftAligned(int gap) {
  QGuiApplication::primaryScreen()->availableGeometry();
  setGeometry(s.x() + gap, s.y() + gap, width(), s.height() - 2 * gap);
  QDialog::show();
}

如您所见,我使用屏幕

availableGeometry
来考虑上部菜单栏或/和下部泊坞窗。就我而言,docker 是隐藏的,但 OSX 菜单设置为始终打开。

此方法的问题在于,生成的对话框的顶部 y 坐标不会将菜单栏下方的

gap
像素分开。这是 32 像素间隙的结果

showLeftAligned result

使用

QGuiApplication::primaryScreen()->geometry()
Qt 可以正确确定屏幕的逻辑尺寸,即
1440 x 900 @ 60.00Hz
。移动到
QGuiApplication::primaryScreen()->geometry()
,我得到
1440 x 875
,由于上方的菜单栏,少了 25 个像素。

所以,这里一切似乎都是合乎逻辑的,但 QDialog 的顶部位置一直是错误的。

我在这里做错了什么?

c++ qt screen qt6
1个回答
2
投票

需要与窗框调整位置。请在此处查看几何图形和框架几何图形之间差异的图片和说明:https://doc.qt.io/qt-6/application-windows.html#window-geometry您的最终代码可能如下所示。

void QXDialog::showLeftAligned(int gap) {
    show(); // we must call this before measuring the window geometry
    auto s = QGuiApplication::primaryScreen()->availableGeometry();
    s.adjust(gap, gap, -gap, -gap);
    auto rect = geometry();
    auto frameRect = frameGeometry();
    auto topLeftDiff = rect.topLeft() - frameRect.topLeft();
    auto bottomRightDiff = rect.bottomRight() - frameRect.bottomRight();
    s.adjust(topLeftDiff.x(), topLeftDiff.y(), bottomRightDiff.x(), bottomRightDiff.y());
    setGeometry(s);
}
© www.soinside.com 2019 - 2024. All rights reserved.