///----------------
MyMenu.qml
///----------------
import QtQuick 2.9
import QtQuick.Window 2.2
import QtQuick.Controls 2.4
MyMenu {
id: root
title: "Menu"
bottomInset: -2
background: Rectangle {
color: "lightblue"
implicitWidth: {
var maxWidth = 0;
for (var i = 0; i < root.count; i++) {
var obj = root.itemAt(i);
maxWidth = Math.max(obj.implicitWidth, maxWidth);
}
return maxWidth;
}
Rectangle {
color: "red"
implicitWidth: parent.width
implicitHeight: 2
anchors.bottom: parent.bottom
}
}
delegate: MyMenuItem { }
}
///----------------
MyMenuItem.qml
///----------------
import QtQuick 2.9
import QtQuick.Window 2.2
import QtQuick.Controls 2.4
MenuItem {
id: root
text: "Item"
implicitWidth: implicitContentWidth + leftPadding + rightPadding
implicitHeight: implicitContentHeight + topPadding + bottomPadding
property color bgColor: "transparent"
property color hlColor: "green"
Component.onCompleted: {
background.color = Qt.binding(function(){ return root.highlighted ? root.hlColor : root.bgColor; });
}
}
///----------------
main.qml
///----------------
import QtQuick 2.9
import QtQuick.Window 2.2
import QtQuick.Controls 2.4
Window {
visible: true
width: 640
height: 480
title: qsTr("Hello World")
MenuBar {
MyMenu {
title: "First"
MyMenuItem {
text: "#1 -> Submenu 1"
}
}
MyMenu {
title: "Second"
MyMenuItem {
text: "#2 -> Submenu 1"
}
MyMenu {
title: "#2 -> Submenu 2 (the most most most long DICK)"
MyMenuItem {
text: "ABC"
}
MyMenuItem {
text: "ABC ABC ABC"
}
}
MyMenuItem {
text: "#2 -> Submenu 3 (the most long)"
}
}
MyMenu {
title: "Third"
MyMenuItem {
text: "#3 -> Submenu 1"
//implicitWidth: 400
implicitHeight: 15
}
}
}
}
#include <iostream>
using namespace std;
int main() {
// your code goes here
return 0;
}