fork download
  1. ///----------------
  2.  
  3. MyMenu.qml
  4.  
  5. ///----------------
  6.  
  7. import QtQuick 2.9
  8. import QtQuick.Window 2.2
  9. import QtQuick.Controls 2.4
  10.  
  11.  
  12. MyMenu {
  13. id: root
  14. title: "Menu"
  15.  
  16. bottomInset: -2
  17. background: Rectangle {
  18. color: "lightblue"
  19. implicitWidth: {
  20. var maxWidth = 0;
  21. for (var i = 0; i < root.count; i++) {
  22. var obj = root.itemAt(i);
  23. maxWidth = Math.max(obj.implicitWidth, maxWidth);
  24. }
  25. return maxWidth;
  26. }
  27.  
  28. Rectangle {
  29. color: "red"
  30. implicitWidth: parent.width
  31. implicitHeight: 2
  32. anchors.bottom: parent.bottom
  33. }
  34. }
  35.  
  36. delegate: MyMenuItem { }
  37. }
  38.  
  39.  
  40. ///----------------
  41.  
  42. MyMenuItem.qml
  43.  
  44. ///----------------
  45.  
  46. import QtQuick 2.9
  47. import QtQuick.Window 2.2
  48. import QtQuick.Controls 2.4
  49.  
  50. MenuItem {
  51. id: root
  52. text: "Item"
  53. implicitWidth: implicitContentWidth + leftPadding + rightPadding
  54. implicitHeight: implicitContentHeight + topPadding + bottomPadding
  55.  
  56. property color bgColor: "transparent"
  57. property color hlColor: "green"
  58.  
  59. Component.onCompleted: {
  60. background.color = Qt.binding(function(){ return root.highlighted ? root.hlColor : root.bgColor; });
  61. }
  62. }
  63.  
  64.  
  65.  
  66. ///----------------
  67.  
  68. main.qml
  69.  
  70. ///----------------
  71.  
  72. import QtQuick 2.9
  73. import QtQuick.Window 2.2
  74. import QtQuick.Controls 2.4
  75.  
  76. Window {
  77. visible: true
  78. width: 640
  79. height: 480
  80. title: qsTr("Hello World")
  81.  
  82. MenuBar {
  83. MyMenu {
  84. title: "First"
  85.  
  86. MyMenuItem {
  87. text: "#1 -> Submenu 1"
  88. }
  89. }
  90.  
  91. MyMenu {
  92. title: "Second"
  93.  
  94. MyMenuItem {
  95. text: "#2 -> Submenu 1"
  96. }
  97.  
  98. MyMenu {
  99. title: "#2 -> Submenu 2 (the most most most long DICK)"
  100.  
  101. MyMenuItem {
  102. text: "ABC"
  103. }
  104.  
  105. MyMenuItem {
  106. text: "ABC ABC ABC"
  107. }
  108. }
  109.  
  110. MyMenuItem {
  111. text: "#2 -> Submenu 3 (the most long)"
  112. }
  113. }
  114.  
  115. MyMenu {
  116. title: "Third"
  117.  
  118. MyMenuItem {
  119. text: "#3 -> Submenu 1"
  120. //implicitWidth: 400
  121. implicitHeight: 15
  122. }
  123. }
  124. }
  125. }
  126.  
  127.  
  128.  
  129. #include <iostream>
  130. using namespace std;
  131.  
  132. int main() {
  133. // your code goes here
  134. return 0;
  135. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp:3:1: error: ‘MyMenu’ does not name a type
 MyMenu.qml
 ^~~~~~
prog.cpp:42:1: error: ‘MyMenuItem’ does not name a type
 MyMenuItem.qml
 ^~~~~~~~~~
prog.cpp:68:1: error: ‘main’ does not name a type
 main.qml
 ^~~~
stdout
Standard output is empty