QML: Qt.labs.platform.MenuItem's icon

1k Views Asked by At

There's a plugin called Qt.labs.platform. Among other things, it provides tray icon with a menu. That menu has a list of MenuItems. Any menu item may have an icon, which would be displayed on the left side of item's text.

There's two ways of setting the icon, none of which work for me:

1) Version 1.0 defined iconSource and iconName properties.

Silently does not work, just shows no icon.

2) Revision 1.1 (declared as Q_REVISION(1)) introduces icon.name, icon.source and icon.mask "sub-properties" (not sure what's the right name for it?)

Fails QML engine with a message:
"MenuItem.icon" is not available in Qt.labs.platform 1.1.

I tried both import Qt.labs.platform 1.1 and 1.0.

Am I missing something in the mechanics of QML revisions or this is a bug in Qt?

A MenuItem is declared in qquickplatformmenuitem_p.h and defined in qquickplatformmenuitem.cpp files.

I'm using ArchLinux, KDE/Plasma. Some other apps (like electron-based) do have their icons in menu showing properly.

UPD Reported as a Qt bug.

1

There are 1 best solutions below

5
cppiscute On

I don't know what exactly you are doing to achieve your goal, because there is no minimal reproducible example.

But take a look at here

import QtQuick 2.12
import QtQuick.Window 2.12
// import Qt.labs.platform 1.1
import QtQuick.Controls 2.12

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

    Rectangle {
        width: 200
        height: 200
        anchors.centerIn: parent

        MenuBar {
            id: menuBar

            Menu {
                id: firstMenu

                MenuItem {
                    icon.name: "download"
                    icon.source: "icons/cloud-down-icon.png"
                }

                MenuItem {
                    text: qsTr("Rewrap Paragraph")
                    onTriggered: rewrapParagraph()
                }
            }
        }
    }
}


Note: put your icon inside the icons folder in the project folder.

If you click on the menu then you should get a dropdown with two items, first one is just an icon without the text (you can include some text by yourself), second item is just a text.