Results 1 to 14 of 14

Thread: QML 'Custom Menu' option moving upwards

  1. #1
    Join Date
    Apr 2013
    Posts
    61
    Qt products
    Qt4
    Platforms
    Windows

    Default QML 'Custom Menu' option moving upwards

    Hi,

    I am trying to create a "menu" in QML with custom data in each option
    For requirements of my application, i need to show it loading the QML file dinamically (Qt.createComponent)
    What I need is to show some fixed options in the bottom part, and when clicked the top one, another options appear below this top option, which keeps in the top
    A simple example. I have this menu:

    Option 4
    Option 2
    Option 1

    And when clicked in Option 4, the menu changes to

    Option 4
    Option 3
    Option 2
    Option 1

    So Option 4 is moved upwards and Option 3 appears

    I would like to have a 'shadow' around all my menu (I added a 'DropShadow' component for that purpose).

    I have this simple test code, where I have a main Rectangle (to be surrounded by the shadow), and 2 Rectangles inside.
    Rect1 for the fixed part (Option 1, Option 2), and Rect2 for the 'movable' part (Option 3, Option 4).
    Rect2 is behind Rect1 (z: -1), and located to have only Option 4 visible, above Option 2.
    When clicked Option 4, Rect2 is moved upwards and all options are visible

    To achieve that, I have to update Rect2 visible height, and main window position ('y'), since main window height depends on this Rect2 visible height...

    I have it working, but it flickes a lot since 2 variables changes ('fixed panel' is moved upwards and back)

    I have also tried with a ParallelAnimation for 2 values, but no success...

    Any idea to have this menu with a smooth movement?

    Thanks


    Main.qml

    Qt Code:
    1. import QtQuick 2.0
    2.  
    3. Rectangle
    4. {
    5. id: window
    6.  
    7. property variant win: undefined;
    8. Component.onCompleted:
    9. {
    10. var component = Qt.createComponent("TestMenu.qml");
    11. win = component.createObject(window, {"x": 500, "y": 500});
    12. win.show();
    13. }
    14. }
    To copy to clipboard, switch view to plain text mode 
    TestMenu.qml:

    Qt Code:
    1. import QtQuick 2.0
    2. import QtQuick.Window 2.1
    3. import QtGraphicalEffects 1.0
    4.  
    5. Window {
    6. id: window
    7. flags: Qt.Tool | Qt.FramelessWindowHint
    8. height: panel.height
    9. color: "transparent"
    10.  
    11. property int radiusShadow: 20
    12. property int iOptionHeight: 30
    13.  
    14. Rectangle {
    15. id: panel
    16. anchors { centerIn: parent}
    17.  
    18. height: menu1.height + menu2.heightVisible + 2*radiusShadow
    19. width: window.width - 2*radiusShadow
    20. color: "transparent"
    21.  
    22. Rectangle {
    23. id: menu1
    24.  
    25. anchors { bottom: parent.bottom; bottomMargin: radiusShadow }
    26. width: parent.width
    27. height: column1.children.length * iOptionHeight
    28.  
    29. Column {
    30. id: column1
    31. anchors.fill: parent
    32. Rectangle {
    33. color: "red";
    34. Text { text: qsTr("option 2") }
    35. height: iOptionHeight; width: parent.width
    36. }
    37. Rectangle {
    38. color: "green";
    39. Text { text: qsTr("option 1") }
    40.  
    41. height: iOptionHeight; width: parent.width
    42. }
    43. }
    44. }
    45.  
    46. Rectangle {
    47. id: menu2
    48.  
    49. property int heightVisible: iOptionHeight
    50.  
    51. anchors { top: parent.top; topMargin: radiusShadow; left: menu1.left }
    52. width: parent.width
    53. height: column2.children.length * iOptionHeight
    54.  
    55. z: -1
    56.  
    57. Column {
    58. id: column2
    59.  
    60. anchors.fill: parent
    61. Rectangle {
    62. id: blue
    63. property bool bOpen: false
    64. color: "blue";
    65. height: iOptionHeight; width: parent.width
    66. Text { text: qsTr("option 4") }
    67.  
    68. MouseArea {
    69. anchors.fill: parent
    70. onClicked: {
    71. blue.bOpen = !blue.bOpen;
    72. panel.showHideMenu2(blue.bOpen);
    73. }
    74. }
    75. }
    76. Rectangle {
    77. color: "pink";
    78. Text { text: qsTr("option 3") }
    79. height: iOptionHeight; width: parent.width
    80. }
    81. }
    82. }
    83.  
    84. function showHideMenu2(bShow)
    85. {
    86. if (bShow)
    87. {
    88. window.y -= iOptionHeight
    89. menu2.heightVisible += iOptionHeight;
    90. }
    91. else
    92. {
    93. window.y += iOptionHeight
    94. menu2.heightVisible -= iOptionHeight;
    95. }
    96. }
    97. }
    98.  
    99. DropShadow {
    100. id: dropShadow
    101. visible: true
    102. anchors.fill: panel
    103. radius: radiusShadow
    104. samples: 24
    105. color: "#40000000"
    106. source: panel
    107. }
    108. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QML 'Custom Menu' option moving upwards

    Are you triggering the animation with a Behavior element?

    Cheers,
    _

  3. #3
    Join Date
    Apr 2013
    Posts
    61
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QML 'Custom Menu' option moving upwards

    I have tried now with Behaviour's in each element, but the effect is still really bad...

    I added
    Behavior on height { NumberAnimation { duration: 500 } }
    in every Rectangle
    and
    Behavior on y { NumberAnimation { duration: 500 } }
    in the main window

    Cheers

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QML 'Custom Menu' option moving upwards

    Does it help if you only do one of these? What about each combination of two?

    Cheers,
    _

  5. #5
    Join Date
    Apr 2013
    Posts
    61
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QML 'Custom Menu' option moving upwards

    I'm afraid I don't understand you...

  6. #6
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QML 'Custom Menu' option moving upwards

    If you have only one of these animations, does it behave well?
    If you try different combinations of two of these animations, are there combinations that work?

    Cheers,
    _

  7. #7
    Join Date
    Apr 2013
    Posts
    61
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QML 'Custom Menu' option moving upwards

    I have tried different options but no sucess, the bottom (fixed) menu1 always moves (although smoothly with animations). And this menu should be always at the bottom, while menu2 is the one that moves up (increasing the main window size, but without affecting menu1).

    Thanks

  8. #8
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QML 'Custom Menu' option moving upwards

    You could try using a ListView instead of two moving elements and adding/removing an element to/from the model when needed, using the view's add/remove transitions handle the animation aspect.

    Cheers,
    _

  9. #9
    Join Date
    Apr 2013
    Posts
    61
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QML 'Custom Menu' option moving upwards

    I thought about that option, but when adding/removing an element from the Lisview (from the model), the main window will not be increased downwards?

  10. #10
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QML 'Custom Menu' option moving upwards

    You could still do that with an animation.

    Cheers,
    _

  11. #11
    Join Date
    Apr 2013
    Posts
    61
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QML 'Custom Menu' option moving upwards

    I have tried with a model and a ListView (code is simpler now), but I don't know where and how insert an 'Animation' or a 'Behaviour' to achieve my target (I have tried several options with no success...)

    The expected effect is that the 1st rectangle moves up when the 2nd appears, so the bottom one keeps in its position (at bottom). But the default behaviour when I add an element to the model is that the list increased the height downwards


    My code:

    Qt Code:
    1. import QtQuick 2.0
    2. import QtQuick.Controls 1.4
    3.  
    4. Rectangle {
    5. id: rootItem
    6. width: 400
    7. height: list.height
    8.  
    9. ListModel {
    10. id: modelRects
    11. ListElement { rectColor: "green" }
    12. ListElement { rectColor: "orange" }
    13. }
    14.  
    15. ListView {
    16. id: list
    17. height: modelRects.count * 30
    18. model: modelRects
    19. delegate: Rectangle {
    20. id: delegate
    21. height: 30
    22. width: rootItem.width
    23. color: rectColor
    24.  
    25. MouseArea {
    26. anchors.fill: parent
    27. onClicked: onRectClicked(index);
    28. }
    29. }
    30. }
    31.  
    32. function onRectClicked(index)
    33. {
    34. if (index == 0)
    35. {
    36. if (modelRects.count == 2)
    37. modelRects.insert(1, {"idRect": 2, "rectColor": "red"});
    38. else
    39. modelRects.remove(1, 1);
    40. }
    41. }
    42. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by ddonate; 29th November 2016 at 12:19.

  12. #12
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QML 'Custom Menu' option moving upwards

    In your first attempt you had both rectangles animated. So only one of them actually is?

    Cheers,
    _

  13. #13
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QML 'Custom Menu' option moving upwards

    In your first attempt you had both rectangles animated. So only one of them actually is?

    Cheers,
    _

  14. #14
    Join Date
    Apr 2013
    Posts
    61
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QML 'Custom Menu' option moving upwards

    Having 2 rectangles was only an option I thought to achieve my target, and I tried to animate both to keep the bottom RECT at the bottom side of the main window, while the upper RECT moves up (so the "hidden" RECT appears in the 2nd position)

    But maybe there is an easier way to get what I need (maybe I am not explaining properly what I need). Now my code is simpler, with the same (bad) result: the bottom RECT moves down, since the main window (in this case a ListView) increases its height downwards by default.

Similar Threads

  1. Browsing search text upwards and downwards
    By YatShan in forum Newbie
    Replies: 2
    Last Post: 6th October 2015, 11:29
  2. trouble when moving dynamically created custom Widgets
    By chocolateSteak in forum Qt Programming
    Replies: 1
    Last Post: 16th September 2013, 00:38
  3. custom widget that contains moving parts
    By cgyan1 in forum Qt Programming
    Replies: 2
    Last Post: 14th February 2012, 15:47
  4. Incorrect option.rect in custom item delegate
    By Jarvis in forum Qt Programming
    Replies: 0
    Last Post: 16th April 2010, 23:48
  5. Moving menu to the right menubar corner
    By maverick_pol in forum Qt Programming
    Replies: 4
    Last Post: 21st March 2008, 05:24

Tags for this Thread

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.