Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. [Solved]Multi-select of TableView
QtWS25 Last Chance

[Solved]Multi-select of TableView

Scheduled Pinned Locked Moved QML and Qt Quick
8 Posts 3 Posters 7.0k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    stereomatching
    wrote on last edited by
    #1

    Can't find any properties or functions to do multi-select
    TableView haven't implemented this function yet?Or I miss something?

    1 Reply Last reply
    0
    • P Offline
      P Offline
      PhilFech
      wrote on last edited by
      #2

      Thank's a lot. setSceneRect(minWidth, minHeight, maxWidth, maxHeight) does the job.

      Best wishes,
      Philipp

      1 Reply Last reply
      0
      • P Offline
        P Offline
        PhilFech
        wrote on last edited by
        #3

        Sorry, I was in the wrong forum :-(

        1 Reply Last reply
        0
        • J Offline
          J Offline
          Jens
          wrote on last edited by
          #4

          Multi select is not yet a built-in feature but I made an example showing how you can do it here:
          http://pastebin.com/NMtvPcpk (MultiSelectTableView)

          1 Reply Last reply
          0
          • J Offline
            J Offline
            Jens
            wrote on last edited by
            #5

            @import QtQuick 2.0
            import QtQuick.Controls 1.0

            TableView {
            id: table

            property var indexes: []
            property int __clicks
            SystemPalette{ id: syspal }
            
            rowDelegate: Rectangle {
                property bool selected: table.__clicks && indexes.indexOf(styleData.row) > -1
                width: parent.width ; height: 18
                color: selected ? syspal.highlight : syspal.base
            }
            
            itemDelegate: Text {
                property bool selected: table.__clicks && indexes.indexOf(styleData.row) > -1
                anchors.fill: parent
                color: selected ? syspal.highlightedText : syspal.text
                anchors.leftMargin: 6
                verticalAlignment: Text.AlignVCenter
                renderType: Text.NativeRendering
                elide: styleData.elideMode
                text: styleData.value
            }
            
            onClicked: {
                var indexAt = indexes.indexOf(row)
                if (indexAt > -1)
                    indexes.splice(indexAt, 1)
                else
                    indexes.push(row)
                __clicks++  // force a re-evaluation of indexes in the delegates
            }
            

            }@

            1 Reply Last reply
            0
            • S Offline
              S Offline
              stereomatching
              wrote on last edited by
              #6

              Thanks, hope that Qt5.2 or Qt5.1.x could offer us a way to do multiselect

              1 Reply Last reply
              0
              • J Offline
                J Offline
                Jens
                wrote on last edited by
                #7

                Eh, It just did... The code above is just using plain Qt 5.1.0

                We cant add any new API until 5.2. (5.1.x is only for bug fixes), but the solution I suggested above should be perfectly valid so there is not really any reason to wait if you need it now. I certainly agree that there should be convenience for it though.

                1 Reply Last reply
                0
                • J Offline
                  J Offline
                  Jens
                  wrote on last edited by
                  #8

                  Added support for key selection and key modifiers
                  @
                  import QtQuick 2.0
                  import QtQuick.Controls 1.0

                  TableView {
                  id: table

                  SystemPalette{ id: syspal }
                  
                  property var indexes: []
                  property int __clicks
                  property int __previousRow: -1
                  property int __modifiers: 0
                  property int __firstKeyRow: -1
                  
                  rowDelegate: Rectangle {
                      property bool selected: table.__clicks && indexes.indexOf(styleData.row) > -1
                      width: parent.width ; height: 18
                      color: selected ? syspal.highlight : syspal.base
                  }
                  
                  itemDelegate: Text {
                      property bool selected: table.__clicks && indexes.indexOf(styleData.row) > -1
                      anchors.fill: parent
                      color: selected ? syspal.highlightedText : syspal.text
                      anchors.leftMargin: 6
                      verticalAlignment: Text.AlignVCenter
                      renderType: Text.NativeRendering
                      elide: styleData.elideMode
                      text: styleData.value
                  }
                  
                  // Shift-key selection is relative to the index when shift was pressed
                  Keys.onUpPressed: keySelect(__modifiers & Qt.ShiftModifier, currentRow)
                  Keys.onDownPressed: keySelect(__modifiers & Qt.ShiftModifier, currentRow)
                  
                  function keySelect(shiftPressed, row) {
                      if (shiftPressed) {
                          indexes = []
                          setRowRange(__modifiers & Qt.ShiftModifier, __firstKeyRow, row)
                      } else {
                          indexes = [row]
                      }
                  }
                  
                  Keys.onPressed: {
                      if (event.key === Qt.Key_Control)
                          __modifiers |= Qt.ControlModifier
                      if (event.key === Qt.Key_Shift) {
                          __modifiers |= Qt.ShiftModifier
                          __firstKeyRow = currentRow
                      }
                  }
                  
                  Keys.onReleased: {
                      if (event.key === Qt.Key_Control)
                          __modifiers &= !Qt.ControlModifier
                      if (event.key === Qt.Key_Shift) {
                          __modifiers &= !Qt.ShiftModifier
                          __firstKeyRow = -1
                      }
                  }
                  
                  onClicked: {
                      var index = currentRow
                      if (__modifiers & Qt.ShiftModifier) {
                          setRowRange(true, __previousRow, index)
                      } else {
                          var hasIndex = indexes.indexOf(index) !== -1
                          if (__modifiers & Qt.ControlModifier) {
                              setRowRange(!hasIndex, index, index)
                          } else {
                              indexes = (indexes.length == 1 && hasIndex) ? [] : [index]
                              __clicks++  // force a re-evaluation of indexes in the delegates
                          }
                      }
                      __previousRow = index
                  }
                  
                  function setRowRange(select, first, last) {
                      var start = first <= last ? first : last
                      var stop = first <= last ? last : first
                      for (var i = start ; i <= stop ; ++ i) {
                          var indexAt = indexes.indexOf(i)
                          if (select) {
                              if (indexAt < 0)
                                  indexes.push(i)
                          } else {
                              if (indexAt > -1)
                                  indexes.splice(indexAt, 1);
                          }
                      }
                      __clicks++  // force a re-evaluation of indexes in the delegates
                  }
                  

                  }
                  @

                  1 Reply Last reply
                  0

                  • Login

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • Users
                  • Groups
                  • Search
                  • Get Qt Extensions
                  • Unsolved