setGeometry fails setting top coordinate using Qt6 on OS X

huangapple go评论67阅读模式
英文:

setGeometry fails setting top coordinate using Qt6 on OS X

问题

I am creating a method called showLeftAligned to force the geometry of a QDialog to be left aligned on screen, separated by a gap of gap pixels around.

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

如您所见,我正在创建一个名为showLeftAligned的方法,以强制QDialog的几何形状在屏幕上左对齐,并在周围以gap像素的间隔分隔开。

请问您是否需要进一步的帮助?

英文:

I am creating a method called showLeftAligned to force the geometry of a QDialog to be left aligned on screen, separated by a gap of gap pixels around.

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

As you can see, I am using the screen availableGeometry to take into account the upper menu bar or/and the lower docker. In my case, docker is hidden but OSX menu is set to be always on.

The problem with this method is that the top y-coordinate of the resulting dialog does not separates gap pixels below the menu bar. Here is the result for a gap of 32 pixels.

setGeometry fails setting top coordinate using Qt6 on OS X

Using QGuiApplication::primaryScreen()->geometry() Qt is getting right the logical size of the screen, which is 1440 x 900 @ 60.00Hz. Moving to QGuiApplication::primaryScreen()->geometry() I get 1440 x 875, 25 pixels less due to the upper menu bar.

So, everything seems to be logical here but the top position of the QDialog keeps being wrong.

What am I doing wrong here?

答案1

得分: 2

你需要调整与窗口框架的位置。请参考此处关于几何和框架几何之间差异的图片和解释:https://doc.qt.io/qt-6/application-windows.html#window-geometry 您的最终代码可以如下所示。

void QXDialog::showLeftAligned(int gap) {
    show(); // 我们必须在测量窗口几何之前调用这个函数
    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);
}
英文:

You need to adjust the position with the window frame. See the picture and explanation of the difference between geometry and frame geometry here: https://doc.qt.io/qt-6/application-windows.html#window-geometry Your final code can look like this.

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);
}

huangapple
  • 本文由 发表于 2023年5月21日 19:50:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/76299773.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定