Results 1 to 3 of 3

Thread: Custom menu

  1. #1
    Join Date
    Feb 2013
    Posts
    4
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Custom menu

    I have a working custom menu component, items are highlighted on mouse-over and change colour to show the user's current selection.
    My current code is given below. Im sure there is a more concise / generic way of handling this, any thoughts or suggestions on how to improve my QML are most welcome.

    Menu.qml
    Qt Code:
    1. import QtQuick 2.4
    2. import QtQuick.Controls 1.3
    3.  
    4. Item
    5. {
    6. width: 100
    7. height: 650
    8.  
    9. Column
    10. {
    11. anchors.verticalCenter: parent.verticalCenter
    12. spacing: 20
    13.  
    14. property int index: -1
    15.  
    16. function setPage(i)
    17. {
    18. if(index != i)
    19. {
    20. index = i;
    21.  
    22. if(index == 0)
    23. {
    24. page0.selected = true;
    25. page1.selected = false;
    26. page2.selected = false;
    27. page3.selected = false;
    28. }
    29.  
    30. if(index == 1)
    31. {
    32. page0.selected = false;
    33. page1.selected = true;
    34. page2.selected = false;
    35. page3.selected = false;
    36. }
    37.  
    38. if(index == 2)
    39. {
    40. page0.selected = false;
    41. page1.selected = false;
    42. page2.selected = true;
    43. page3.selected = false;
    44. }
    45.  
    46. if(index == 3)
    47. {
    48. page0.selected = false;
    49. page1.selected = false;
    50. page2.selected = false;
    51. page3.selected = true;
    52. }
    53. }
    54. }
    55.  
    56. MyButton
    57. {
    58. id: page0
    59. text: "A"
    60. onClicked: parent.setPage(0)
    61.  
    62. }
    63.  
    64. MyButton
    65. {
    66. id: page1
    67. text: "B"
    68. onClicked: parent.setPage(1)
    69. }
    70.  
    71. MyButton
    72. {
    73. id: page2
    74. text: "C"
    75. onClicked: parent.setPage(2)
    76. }
    77.  
    78. MyButton
    79. {
    80. id: page3
    81. text: "D"
    82. onClicked: parent.setPage(3)
    83. }
    84.  
    85. Component.onCompleted: setPage(0)
    86. }
    87. }
    To copy to clipboard, switch view to plain text mode 

    MyButton.qml
    Qt Code:
    1. import QtQuick 2.4
    2. import QtQuick.Controls 1.3
    3.  
    4. Item {
    5. id: menuButton
    6.  
    7. width:100
    8. height:100
    9.  
    10. property string text: ""
    11. property bool selected: false
    12.  
    13. signal clicked
    14.  
    15. Rectangle {
    16. anchors.fill: parent
    17. color: selected == true ? "#008080" : "white"
    18. border.width: 4
    19. border.color: mouseArea.containsMouse ? "black" : "#007070"
    20. }
    21.  
    22. Text {
    23. anchors.centerIn: parent
    24. text: parent.text
    25. color: selected == true ? "white" : "#008080"
    26. font.family: "Roboto-Medium"
    27. font.pointSize: 12
    28. font.weight: Font.Normal
    29. elide: Text.ElideRight
    30. width: parent.width
    31. horizontalAlignment: Text.AlignHCenter
    32. anchors.margins: 4
    33. }
    34.  
    35. MouseArea
    36. {
    37. id: mouseArea
    38. anchors.fill: parent
    39. hoverEnabled: true
    40. Component.onCompleted: clicked.connect(menuButton.clicked)
    41. }
    42. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,376
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    4
    Thanked 5,019 Times in 4,795 Posts
    Wiki edits
    10

    Default Re: Custom menu

    Your spaghetti if blocks are not the prettiest ones. You made your code totally imperative which is not necessary.

    Your code can be rewritten as:
    javascript Code:
    1. import QtQuick 2.4
    2. import QtQuick.Controls 1.3
    3.  
    4. Item
    5. {
    6. width: 100
    7. height: 650
    8.  
    9. Column
    10. {
    11. id: col
    12. anchors.verticalCenter: parent.verticalCenter
    13. spacing: 20
    14.  
    15. property int index: -1
    16.  
    17. function setPage(i)
    18. {
    19. col.index = i
    20. }
    21.  
    22. MyButton
    23. {
    24. id: page0
    25. selected: col.index == 0
    26. text: "A"
    27. onClicked: parent.setPage(0)
    28.  
    29. }
    30.  
    31. MyButton
    32. {
    33. id: page1
    34. selected: col.index == 1
    35. text: "B"
    36. onClicked: parent.setPage(1)
    37. }
    38.  
    39. MyButton
    40. {
    41. id: page2
    42. selected: col.index == 2
    43. text: "C"
    44. onClicked: parent.setPage(2)
    45. }
    46.  
    47. MyButton
    48. {
    49. id: page3
    50. selected: col.index == 3
    51. text: "D"
    52. onClicked: parent.setPage(3)
    53. }
    54.  
    55. Component.onCompleted: setPage(0)
    56. }
    57. }
    To copy to clipboard, switch view to plain text mode 

    or even shorter:

    javascript Code:
    1. import QtQuick 2.4
    2. import QtQuick.Controls 1.3
    3.  
    4. Item
    5. {
    6. width: 100
    7. height: 650
    8.  
    9. Column
    10. {
    11. id: col
    12. anchors.verticalCenter: parent.verticalCenter
    13. spacing: 20
    14.  
    15. property int index: 0
    16.  
    17. Repeater {
    18. model: [ "A", "B", "C", "D" ]
    19.  
    20. MyButton
    21. {
    22. selected: col.index == index
    23. text: modelData
    24. onClicked: col.index = index
    25.  
    26. }
    27. }
    28. }
    To copy to clipboard, switch view to plain text mode 
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Feb 2013
    Posts
    4
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Custom menu

    Thanks for your quick response. I really like your second method using the repeater, the code becomes very compact.

Similar Threads

  1. Custom Menu for File Entries
    By franku in forum Qt Programming
    Replies: 3
    Last Post: 7th July 2011, 17:56
  2. Custom menu widget
    By shadowfax in forum Newbie
    Replies: 0
    Last Post: 9th July 2009, 19:30
  3. Custom context menu in QTreeView
    By ttvo in forum Qt Programming
    Replies: 5
    Last Post: 3rd April 2009, 23:29
  4. Replies: 7
    Last Post: 23rd March 2009, 22:01
  5. image for a custom menu Item.......
    By Naveen in forum Qt Programming
    Replies: 2
    Last Post: 23rd February 2006, 10:28

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.