【问题标题】:Menu bar attached to window. Qt 6 / macOS附加到窗口的菜单栏。 Qt 6 / macOS
【发布时间】:2022-08-20 07:55:14
【问题描述】:

我已将 Mac 程序的 qt 版本更新到 6.3,现在位于菜单栏在窗口的顶部边缘,而不是原生的顶部 macOS 栏。如何将菜单移动到“苹果图标”旁边的 macOS 屏幕顶部? 这是我的代码。它适用于 qt 5.15。

ApplicationWindow {
    id: window
    width: 320
    height: 260
    visible: true

    menuBar: MenuBar {
        Menu {
            title: qsTr(\"&File\")
            Action { text: qsTr(\"&New...\") }
            Action { text: qsTr(\"&Open...\") }
            Action { text: qsTr(\"&Save\") }
            Action { text: qsTr(\"Save &As...\") }
            MenuSeparator { }
            Action { text: qsTr(\"&Quit\") }
        }
        Menu {
            title: qsTr(\"&Edit\")
            Action { text: qsTr(\"Cu&t\") }
            Action { text: qsTr(\"&Copy\") }
            Action { text: qsTr(\"&Paste\") }
        }
        Menu {
            title: qsTr(\"&Help\")
            Action { text: qsTr(\"&About\") }
        }
    }
}

标签: macos qt qml menubar


【解决方案1】:

为此有一个特殊的窗口标志。尝试以下操作:

ApplicationWindow {
    width: 320
    height: 260
    visible: true
    flags: ~Qt.AA_DontUseNativeMenuBar
    menuBar: MenuBar {...}
}

【讨论】:

  • 在 Qt 6.3.1 中,这会使标题栏消失,但菜单仍保留在窗口上! :-(
【解决方案2】:

使用 Qt.labs.platform MenuBar 而不是默认的:

import Qt.labs.platform as Labs
...
ApplicationWindow {
    ...
    property Labs.MenuBar nativeMenu: Labs.MenuBar  {
        id: nativeMenuBar
        Labs.Menu {
            id: fileMenu
            title: qsTr("&File")
            Labs.MenuItem {
                text: qsTr("&New")
                onTriggered: doNew()
            }
            Labs.MenuItem {
                text: qsTr("&Open")
                onTriggered: fileOpenDialog.open()
            }
            ...
        }
        Labs.Menu {
            id: editMenu
            title: qsTr("&Edit")
            ...
        }
        ...
    }
    ...
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-03-11
    • 1970-01-01
    • 2013-01-27
    • 1970-01-01
    • 1970-01-01
    • 2019-02-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多