peter9477

snippet for doing mutually exclusive selection in ListView

Jan 16th, 2013
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ListView {
  2.     property variant selectedPath: []
  3.     ...
  4.  
  5.                     onSelectionChanged: {
  6.                         if (selected)
  7.                             selectedPath = indexPath;
  8.                         else
  9.                             selectedPath = [];
  10.                     }
  11.  
  12.                     onTriggered: {
  13.                         if (equalPaths(selected(), indexPath)) {
  14.                             select(indexPath, false);
  15.                         }
  16.                         else {
  17.                             clearSelection();
  18.                             select(indexPath, true);
  19.                         }
  20.                     }
  21.  
  22.  
  23. //--------------------------------------
  24. // Compare two indexPaths as returned by ListView.selected()
  25. // and other routines, returning true only if they're identical.
  26. //
  27. function equalPaths(ip1, ip2) {
  28.     if (!ip1 || !ip2)
  29.         return false;
  30.  
  31.     if (ip1.length != ip2.length)
  32.         return false;
  33.  
  34.     for (var i = 0; i < ip1.length; i++)
  35.         if (ip1[i] != ip2[i])
  36.             return false;
  37.  
  38.     return true;
  39. }
Advertisement
Add Comment
Please, Sign In to add comment