c++ - 当主窗口保持在顶部时,子窗口被遮挡 - MacOS

标签 c++ qt qt5

通常,工具窗口总是位于主窗口的顶部,即使它失去了焦点。然而。

在 MacOS 上,当主窗口设置窗口标志 Qt::WindowStaysOnTopHint 时,工具窗口位于其下方。

在 Windows 系统上,工具窗口按预期位于主窗口的顶部。

有没有办法让主窗口保持在顶部,而工具窗口在它上面?

enter image description here

我当前的 Qt 版本是 5.9.6 和 5.12.1。 MacOS 版本为 10.13.4。

这是我的测试代码

#include <QtGui>
#include <QtWidgets>

int main(int argc, char * argv[])
{
    QApplication app(argc, argv);

    QDialog* mw = new QDialog(0, Qt::WindowStaysOnTopHint | Qt::X11BypassWindowManagerHint);
    mw->setWindowTitle(QLatin1String("Main"));
    mw->resize(400, 300);
    mw->move(100, 100);

    QWidget* d = new QWidget(mw, Qt::Tool);
    d->setWindowTitle(QLatin1String("Tool"));
    d->resize(200, 100);
    d->show();
    mw->exec();
    return app.exec();
}

最佳答案

抱歉,我没有 MacOS 系统,但是,我可以在 Ubuntu 下重现并修复您的问题。希望它在 MacOS 下也能正常工作。

首先,bnaecker 是对的,您应该为主窗口调用 show() 而不是 exec()。如果它是 QMainWidget 而不是 QDialog 就好了,但这不是这里的问题。

根据文档:

Qt::Tool: Indicates that the widget is a tool window. A tool window is often a small window with a smaller than usual title bar and decoration, typically used for collections of tool buttons. It there is a parent, the tool window will always be kept on top of it. If there isn't a parent, you may consider using Qt::WindowStaysOnTopHint as well. If the window system supports it, a tool window can be decorated with a somewhat lighter frame. It can also be combined with Qt::FramelessWindowHint.

您的工具小部件具有父级,因此应该可以工作。但是,通过测试,我可以看到当显示工具本身时应该显示该工具的窗口,以便它按预期保持在顶部...这很奇怪,但这是我观察到的。

另外,我注意到 Qt::X11BypassWindowManagerHint 把事情搞砸了......

最后,通过结合这些观察 + bnaecker 回复,代码是:

#include <QtGui>
#include <QApplication>

int main(int argc, char * argv[])
{
    QApplication app(argc, argv);

    // don't set Qt::X11BypassWindowManagerHint if you want the tool to stay on top!
    QDialog* mw = new QDialog(0, Qt::WindowStaysOnTopHint);// | Qt::X11BypassWindowManagerHint);
    mw->setWindowTitle(QLatin1String("Main"));
    mw->resize(400, 300);
    mw->move(100, 100);

    // show main window before tool is shown
    mw->show();

    QWidget* d = new QWidget(mw, Qt::Tool);
    d->setWindowTitle(QLatin1String("Tool"));
    d->resize(200, 100);
    d->show();

    return app.exec();
}

现在,“工具”窗口位于“主”对话框的顶部,父级无法隐藏它。

请注意,无论您是否为“主”窗口指定 Qt::WindowStaysOnTopHint 标志,此操作都有效。此标志对“Tool”保留在“Main”之上这一事实没有影响。这使得“Main”(以及“Tool”)位于任何其他应用程序窗口的顶部。

关于c++ - 当主窗口保持在顶部时,子窗口被遮挡 - MacOS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54452889/

相关文章:

c++ - OpenGl 和 QT 坐标

c++ - 对齐大小数组和非对齐大小数组的速度不同

c++ - 链接静态库会导致链接库出错

c++ - C++ GUI 编程的建议

c++ - 如何在带有背景图像的小部件上制作不可见按钮?

qt - 如何在QML中声明列表属性

qt - QNetworkRequest::User 和 QNetworkRequest::UserMax 的区别

c++ - 通过 const 成员变量访问内联函数

c++ - 将 QString 转换为 char* 以用于第 3 方库的内存清理问题,如何解决?

c++ - Qt 中的自定义和默认消息处理程序