Home > Net >  QML on Windows: use platform menu in dark mode, or get qt menu to jump to underlines?
QML on Windows: use platform menu in dark mode, or get qt menu to jump to underlines?

Time:01-01

Using both Qt 6.3.0 and 6.4.1, I'm trying to use a menu bar on Windows that (a) renders in dark mode and (b) lets the user jump to a menu item by typing its underlined character, just like a normal Windows app does.

If I use qt.labs platform menus, I can't find a way to make (a) happen, but (b) happens by default.

If I use qt's own menus, (a) happens naturally, but (b) does not.

I set up dark mode in main.cpp as follows (based on this post, among others):

#ifdef Q_OS_WIN
QSettings settings("HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\Themes\\Personalize",QSettings::NativeFormat);
if(settings.value("AppsUseLightTheme")==0){
    app.setStyle(QStyleFactory::create("Fusion"));
    QPalette darkPalette;
    QColor darkColor = QColor(64,64,64); // QColor(45,45,45);
    QColor disabledColor = QColor(127,127,127);
    darkPalette.setColor(QPalette::Window, darkColor);
    darkPalette.setColor(QPalette::WindowText, Qt::white);
...
    darkPalette.setColor(QPalette::Disabled, QPalette::HighlightedText, disabledColor);

    app.setPalette(darkPalette);

    app.setStyleSheet("QToolTip { color: #ffffff; background-color: #2a82da; border: 1px solid white; }");
}
#endif

My QML code is this, for platform menus:

import Qt.labs.platform as Platform

ApplicationWindow {
width: 640
height: 480
visible: true
title: qsTr("Hello World")

Platform.MenuBar  {
    id: menuBar
    Platform.Menu {
        id: fileMenu
        title: qsTr("&File")

         Platform.MenuItem {
            id: openMenuItem;
            text: qsTr("&Open...")
            onTriggered: fileOpenAction.trigger();
        } // open
    }
}
...

Or this, for native menus:

ApplicationWindow {
width: 640
height: 480
visible: true
title: qsTr("Hello World")

menuBar: MenuBar  {
    id: menuBar
    Menu {
        id: fileMenu
        title: qsTr("&File")

         MenuItem {
            id: openMenuItem;
            text: qsTr("&Open...")
            onTriggered: fileOpenAction.trigger();
        } // open
    }
}
...

In both cases, the "Open..." item displays with an underlined O.

With the platform menus, the menu bar and menus are resolutely black on white, even as the rest of the window goes dark (explicit changes to the parent's palette aren't seen either, and the platform menu elements don't have a palette member that can be modified directly). I can type Alt F to display the menu, and O to select the Open item. Is there a way to make the Windows-native platform menu respect dark mode?

With qt menus, the menu turns dark in dark mode, and I can type Alt F to display the menu, but typing O does nothing. Is there a way to make the qt menus select a menu item when its underlined character is typed?

CodePudding user response:

You can change the color of your controls by modifying the palette. The modification of the palette is hierarchical. i.e. you can define the palette by setting it in the parent control and the children will inherit, or, you can define the palette in the child control.

Here's a working example:

import QtQuick
import QtQuick.Controls
Page {
    background: Rectangle { color: "#444" }
    palette.buttonText: "lightgreen" // menu text
    palette.button:     "black"      // menu background
    palette.mid:        "grey"       // menu highlighted background
    palette.windowText: "lightgreen" // popup menu text
    palette.window:     "black"      // popup menu background
    palette.light:      "grey"       // popup menu highlighted background
    palette.dark:       "grey"       // popup menu border
    MenuBar  {
        Menu {
            title: qsTr("&File")
            MenuItem {
                text: qsTr("&Open...")
            }
            MenuItem {
                text: qsTr("&Quit")
            }
        }
        Menu {
            title: qsTr("&Edit")
            palette.window: "#400"
            MenuItem {
                text: qsTr("&Copy")
            }
            MenuItem {
                text: qsTr("&Paste")
            }
        }
    }
}

You can Try it Online!

References:

CodePudding user response:

"Solved" by talamaki in the comments: Pressing the letter alone is no longer that way it's done; the proper way is to press Alt letter, and that works with qt menus — which obey dark mode. Thanks, talamaki!

  • Related