Results 1 to 10 of 10

Thread: How to remember and reestablish the last currentIndex position of each ListElement?

  1. #1
    Join Date
    Sep 2015
    Posts
    32
    Thanks
    5
    Qt products
    Qt5
    Platforms
    Windows

    Default How to remember and reestablish the last currentIndex position of each ListElement?

    Hi,

    I'm Jay and even though I was always around computers, I am completely new to QT with some knowledge and background in coding but rather small to be honest.

    Background stuff:
    I have an arcade cab running with a PC backbone.
    I have a very specific need for a FrontEnd that will allow me to browse/access easily the different categories and different titles (i.e games) in each category.
    I used a QT caroussel to achieve that. A friend is helping me out.



    Here is my problem:
    I have a ListModel that contains 2 ListElements
    Each ListElement is populated with all the "*.png" of a folder (two different folders, one contains 5 png files, the other 42 png files)

    Pressing Up or Down switches from one ListElement to the other (they're called TARGET and VERSUS)
    Pressing LEFT or RIGHT switches, while positioned on a specific ListElement, from one item to the next (TARGET contains 5 items, VERSUS contains 42)

    With the code below, when I press LEFT or RIGHT, it affects the position of BOTH ListElements, regardless of that ListElement being visible or not.
    Even though only one ListElement is visible/active, the other IS affected and when I switch back to it, the position/currentIndex (i.e the PNG highlighted) CHANGED compared to the last time we browsed THIS ListElement.

    I guess it's the basic behavior of QT.



    I want my code to do the following:
    For each ListElement (TARGET and VERSUS), always remember the position we're at in order to:
    Restore/Refresh that specific position when we cycle BACK to this SPECIFIC ListElement

    Demo:
    we're at position 3 on TARGET.
    we switch to VERSUS.
    we go crazy with LEFTs and RIGHTs.
    we go back to TARGET
    TARGET automatically switches back to last known position : 3




    Here is what I tried with global variables initially set at 0 (one for each ListElement):

    Qt Code:
    1. Item {
    2.  
    3. // verylow res, using native arcade system capabilities
    4. width: 640
    5. height: 240
    6.  
    7.  
    8. //We start at position 0 of each ListElement, i.e Category
    9. property int targetSubTypePosition: 0
    10. property int versusSubTypePosition: 0
    11.  
    12. property int indexSubTypePath: 0
    13.  
    14. color: "black"
    15.  
    16. //Populates the 2 ListElements, each with the content of a folder. We only scan for .png
    17. ListModel {
    18. id: subTypeModel
    19. ListElement { name: "YOKO_TG_"; path: "file:///D:/ArcadeLauncher 25.04.2015/DEV_ARCADE/YOKO_TG_/" ; position: 0}
    20. ListElement { name: "YOKO_VS_"; path: "file:///D:/ArcadeLauncher 25.04.2015/DEV_ARCADE/YOKO_VS_/" ; position: 0}
    21. }
    22.  
    23. PathView {
    24. id: gameListView
    25. pathItemCount: 14
    26. anchors.fill: parent
    27. focus: true
    28.  
    29. //Go to next PNG
    30. Keys.onLeftPressed: incrementCurrentIndex()
    31.  
    32. //Go to previous PNG
    33. Keys.onRightPressed: decrementCurrentIndex()
    34.  
    35. //Switch to next category. They cycle around. The first "if" makes sure it cycles
    36. Keys.onUpPressed: {
    37. if (indexSubTypePath == subTypeModel.count -1) {
    38. indexSubTypePath = 0;
    39. } else {
    40. indexSubTypePath++;
    41. }
    42.  
    43. //This is where I "try" to save the last known position of the category that we leave, then I "try" to restore the last known position of the category that we reach
    44. if (indexSubTypePath == 0) {
    45. targetSubTypePosition = currentIndex;
    46. currentIndex = versusSubTypePosition;
    47. } else {
    48. versusSubTypePosition = currentIndex;
    49. currentIndex = targetSubTypePosition;
    50. }
    51.  
    52. }
    53.  
    54. //Switch to next category. They cycle around. The first "if" makes sure it cycles
    55. Keys.onDownPressed: {
    56. if (indexSubTypePath == 0) {
    57. indexSubTypePath = subTypeModel.count -1;
    58.  
    59. } else {
    60. indexSubTypePath--;
    61. }
    62.  
    63. //This is where I "try" to save the last known position of the category that we leave, then I "try" to restore the last known position of the category that we reach
    64. if (indexSubTypePath == 0) {
    65. targetSubTypePosition = currentIndex;
    66. currentIndex = versusSubTypePosition;
    67. } else {
    68. versusSubTypePosition = currentIndex;
    69. currentIndex = targetSubTypePosition;
    70. }
    71. }
    72.  
    73. Keys.onSpacePressed: {
    74. positionViewAtIndex(currentIndex, ListView.SnapPosition);
    75. }
    76.  
    77. model : gameListModel
    78. FolderListModel {
    79. id: gameListModel
    80. nameFilters: ["*.png"]
    81. folder: subTypeModel.get(indexSubTypePath).path//"file:///D:/ArcadeLauncher 25.04.2015/DEV_ARCADE/YOKO_TG_/"
    82. }
    To copy to clipboard, switch view to plain text mode 


    This is my full code (the whole project):
    https://www.dropbox.com/sh/rv7nz858c...8M8KN8yOa?dl=0

    Yours,
    --Jay
    Last edited by JaySDC; 7th September 2015 at 10:43.

  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: How to remember and reestablish the last currentIndex position of each ListElemen

    Hmm.

    As a quick option I would try to have two path views, each one operating on its own data.
    The up/down arrows would then simply switch which one of the two is visible.

    Cheers,
    _

  3. #3
    Join Date
    Sep 2015
    Posts
    32
    Thanks
    5
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: How to remember and reestablish the last currentIndex position of each ListElemen

    Thanks for the help and good idea.

    How do I change this code:

    Qt Code:
    1. Keys.onUpPressed: {
    2. if (indexSubTypePath == subTypeModel.count -1) {
    3. indexSubTypePath = 0;
    4. } else {
    5. indexSubTypePath++;
    6. }
    To copy to clipboard, switch view to plain text mode 

    to switch Path instead?

    Yours,
    --Jay


    Added after 6 minutes:


    Wait, that means I also have to duplicate the model, the path, and the delegate?!

    Right now I am trying with 2 categories only but in the end I will have about 10 of them, that means almost 10 times the same code...
    I am not concerned by having my code all nice and clean but that seems like an extreme quickfix : ).

    Any other solution that would involve just one code and a dynamic variable handling?

    Yours,
    --Jay
    Last edited by JaySDC; 7th September 2015 at 12:16.

  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: How to remember and reestablish the last currentIndex position of each ListElemen

    Quote Originally Posted by JaySDC View Post
    How do I change this code:

    Qt Code:
    1. Keys.onUpPressed: {
    2. if (indexSubTypePath == subTypeModel.count -1) {
    3. indexSubTypePath = 0;
    4. } else {
    5. indexSubTypePath++;
    6. }
    To copy to clipboard, switch view to plain text mode 

    to switch Path instead?
    You would switch the current path view's visible property to false and the other path view's visible property to true.

    Or you have am "current index" property on the encapsulating item and let the two views bind their visible property to that and compare with their index value.

    Quote Originally Posted by JaySDC View Post
    Wait, that means I also have to duplicate the model, the path, and the delegate?!
    No.
    Your PathView basically has one input, a directory.
    You can easily move the PathView code to a file, e.g. GameListView.qml, add a propery for the directory and use that element twice.

    Quote Originally Posted by JaySDC View Post
    Right now I am trying with 2 categories only but in the end I will have about 10 of them, that means almost 10 times the same code...
    I am not concerned by having my code all nice and clean but that seems like an extreme quickfix : ).
    Well, it is a quick fix for the situation you had described
    And it does not require code duplication, just creating a custom component.

    Quote Originally Posted by JaySDC View Post
    Any other solution that would involve just one code and a dynamic variable handling?
    Your initial attempt most likely failed because you are "resetting" the current index too soon, i.e. right after the switch but before the model had any chance of loading the new content.
    For that you would have to delay resetting the index until the model has completed loading (or at least loaded up to the desired index), or keep the models loaded and switch model as well as current index.

    Hence the idea of just keeping the whole view around and see how that goes.

    Cheers,
    _

  5. #5
    Join Date
    Sep 2015
    Posts
    32
    Thanks
    5
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: How to remember and reestablish the last currentIndex position of each ListElemen

    Well thank you very much with all that!

    I'll see what I can do with my friend.

    Even tough I understand what you say and the philosophy behind it, my coding knowledge is too limited to create the code. My skill can only try and customize an existing code based on examples.

    : )

    --Jay

  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: How to remember and reestablish the last currentIndex position of each ListElemen

    Quote Originally Posted by JaySDC View Post
    Even tough I understand what you say and the philosophy behind it, my coding knowledge is too limited to create the code. My skill can only try and customize an existing code based on examples.
    Right, that's one of the reasons I suggested the simpler approach.

    Creating a custom component in QML is very easy.
    Basically you just copy part of the code you have to a new QML file for which you use a filename that matches the name of how you want to call your component in the QML file using it.

    E.g. your code conceptually looks like this

    Qt Code:
    1. import QtQuick 2.4
    2.  
    3. Item {
    4. ListModel {
    5. }
    6.  
    7. PathView {
    8. ....
    9. }
    10. }
    To copy to clipboard, switch view to plain text mode 
    You then copy all code of that PathView section to a new file along side the original file, e.g. GameListView.qml
    Qt Code:
    1. import QtQuick 2.4
    2.  
    3. PathView {
    4. ....
    5. }
    To copy to clipboard, switch view to plain text mode 
    The original file can then be changed to
    Qt Code:
    1. import QtQuick 2.4
    2.  
    3. Item {
    4. ListModel {
    5. }
    6.  
    7. GameListView {
    8. }
    9. }
    To copy to clipboard, switch view to plain text mode 
    That does not work at this point because you have been referencing the list model inside the PathView.
    But in fact you only needed the directory. so you add a property for that in GameListView.qml
    Qt Code:
    1. PathView {
    2. property string directory
    3.  
    4. ....
    5.  
    6. FolderListModel {
    7. id: gameListModel
    8. nameFilters: ["*.png"]
    9. folder: parent.directory
    10. }
    11. }
    To copy to clipboard, switch view to plain text mode 
    You can then use multiple GameListView instances
    Qt Code:
    1. Item {
    2. property int currentSubType: 0
    3.  
    4. GameListView {
    5. visible: parent.currentSubType === 0
    6. directory: subTypeModel.get(0).path;
    7. }
    8. GameListView {
    9. visible: parent.currentSubType === 1
    10. directory: subTypeModel.get(1).path;
    11. }
    12. }
    To copy to clipboard, switch view to plain text mode 

    Cheers,
    _

  7. #7
    Join Date
    Sep 2015
    Posts
    32
    Thanks
    5
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: How to remember and reestablish the last currentIndex position of each ListElemen

    Thank you very much once again : ) Cheers for the time you spent on helping me out, you earned a lot of Karma points I'm sure ; )

    I'll give all this a go tomorrow morning with fresh eyes!!

  8. #8
    Join Date
    Sep 2015
    Posts
    32
    Thanks
    5
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: How to remember and reestablish the last currentIndex position of each ListElemen

    Hmm it is not working yet.

    Where shall I put all the code concerning the actual Path ?

    Qt Code:
    1. path: Path {
    2. id:myPath
    3.  
    4. // CURRENT WORKING
    5. // Path
    6.  
    7. // Premiere position (LENTILLE)
    8. startX: 95; startY: 160
    9.  
    10. // Differents points de passage QUAD
    11. PathQuad { x: 43; y: 53 ; controlX: 18; controlY: 78}
    12. PathAttribute { name: "angle"; value: itemAngle }
    13.  
    14. PathQuad { x: 180; y: 22 ; controlX: 108; controlY: 26}
    15. PathAttribute { name: "angle"; value: itemAngle*2 }
    16.  
    17. PathQuad { x: 320; y: 19 ; controlX: 250; controlY: 21}
    18. PathAttribute { name: "angle"; value: itemAngle*3 }
    19.  
    20. PathQuad { x: 460; y: 22 ; controlX: 390; controlY: 21}
    21. PathAttribute { name: "angle"; value: itemAngle*4 }
    22.  
    23. PathQuad { x: 597; y: 53 ; controlX: 528; controlY: 37}
    24. PathAttribute { name: "angle"; value: itemAngle*5 }
    25.  
    26. PathQuad { x: 545; y: 159 ; controlX: 571; controlY: 106}
    27. PathAttribute { name: "angle"; value: itemAngle*6 }
    28.  
    29.  
    30. }
    To copy to clipboard, switch view to plain text mode 


    Added after 16 minutes:


    Haha nevermind I'm lost.

    I'll have my friend have a look to help me out.

    Cheers though,
    --Jay
    Last edited by JaySDC; 8th September 2015 at 09:45.

  9. #9
    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: How to remember and reestablish the last currentIndex position of each ListElemen

    I am not sure I understand the question.
    "path" is a propery of the PathView, so that code needs to be inside the Path element unless you want each instance to use a different path.

    Cheers,
    _

  10. #10
    Join Date
    Sep 2015
    Posts
    32
    Thanks
    5
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: How to remember and reestablish the last currentIndex position of each ListElemen

    Yes nevermind that last one, my understanding is too limited for me to ask pertinent questions : P.

    I'll check with my friend about the whole thing you proposed and then I might come and ask more precisions.

    Thanks,
    --Jay

Similar Threads

  1. how does qt remember toolbar locations
    By ravas in forum Newbie
    Replies: 4
    Last Post: 14th July 2015, 21:03
  2. ListElement refer ListModel id
    By keolsen in forum Qt Quick
    Replies: 0
    Last Post: 13th November 2012, 11:23
  3. Replies: 0
    Last Post: 22nd April 2011, 11:20
  4. remember file path
    By eric in forum Qt Programming
    Replies: 2
    Last Post: 1st April 2008, 17:52
  5. currentIndex().internalPointer() problem
    By xgoan in forum Qt Programming
    Replies: 2
    Last Post: 14th December 2006, 10:55

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.