Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Treeview + QFileSystemModel, how can i retrieve multi selection list
QtWS25 Last Chance

Treeview + QFileSystemModel, how can i retrieve multi selection list

Scheduled Pinned Locked Moved Solved General and Desktop
16 Posts 4 Posters 2.4k 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.
  • B blossomsg
    22 Sept 2020, 13:11

    Hey Thanks for the snippet @Gojir4 ,
    the extended selection works fine, but if now i want to print every item that i have selected randomly how can i do that?

    eg:
    D:/
    a
    b
    c
    e
    f

    so then if i select a, c, e, f-- how can i return selection values of all four items as strings
    any approach?

    G Offline
    G Offline
    Gojir4
    wrote on 23 Sept 2020, 08:07 last edited by
    #6

    @blossomsg Using i.data() instead of i.row()

    I have updated my previous post to display data. Note with QFileSystemModel you probably need to use QFileSystemModel.FilePathRole role instead of default Qt.DisplayRole. see https://doc.qt.io/qt-5/qfilesystemmodel.html#Roles-enum

    So instead of i.data(), it will be i.data(QtWidgets.QFileSystemModel.FilePathRole)

    def selectionChanged(selected, deselected):
        ...
        # Displaying Row index 
        print('full selection: {}'.format(','.join(str(i.row()) for i in selectionModel.selectedIndexes())))
        # Displaying data
        print('full selection text: {}'.format(','.join(str(i.data()) for i in selectionModel.selectedIndexes())))
    

    Outputs:

    full selection: 1,2,0,1
    full selection text: Row 2,Row 3,Row 2 1,Row 2 2
    

    For converting list of QModelIndex to string list:

    stringlist = [str(i.data()) for i in selectionModel.selectedIndexes()]
    # or simpler
    stringlist = []
    for i in selectionModel.selectedIndexes():
        stringlist.append(str(i.data()))
    
    1 Reply Last reply
    0
    • B Offline
      B Offline
      blossomsg
      wrote on 23 Sept 2020, 15:26 last edited by
      #7

      Hey @Gojir4 ,

      This is nearly what i want, i just don't understand one line
      this some override

      selectionModel: QItemSelectionModel = tree.selectionModel()

      is this python3, or only pyside2

      can you explain, as i have not come across this approach

      J 1 Reply Last reply 23 Sept 2020, 15:36
      0
      • B blossomsg
        23 Sept 2020, 15:26

        Hey @Gojir4 ,

        This is nearly what i want, i just don't understand one line
        this some override

        selectionModel: QItemSelectionModel = tree.selectionModel()

        is this python3, or only pyside2

        can you explain, as i have not come across this approach

        J Offline
        J Offline
        JonB
        wrote on 23 Sept 2020, 15:36 last edited by JonB
        #8

        @blossomsg
        selectionModel: QItemSelectionModel = tree.selectionModel()

        I haven't used it myself, but I believe in later Python 3 you can (optionally) write : type-specification when you create a variable. I use the : type-specification only for the parameters received by methods. This is not a PySide2 thing.

        If you don't like it, or your Python doesn't, it should be just:
        selectionModel = tree.selectionModel()

        It only affects the reading/editing experience anyway, has no effect at runtime.

        G 1 Reply Last reply 23 Sept 2020, 15:42
        1
        • J JonB
          23 Sept 2020, 15:36

          @blossomsg
          selectionModel: QItemSelectionModel = tree.selectionModel()

          I haven't used it myself, but I believe in later Python 3 you can (optionally) write : type-specification when you create a variable. I use the : type-specification only for the parameters received by methods. This is not a PySide2 thing.

          If you don't like it, or your Python doesn't, it should be just:
          selectionModel = tree.selectionModel()

          It only affects the reading/editing experience anyway, has no effect at runtime.

          G Offline
          G Offline
          Gojir4
          wrote on 23 Sept 2020, 15:42 last edited by
          #9

          @JonB Exact
          @blossomsg That's only for my editor knowing the type and providing auto-completion, sry for confusion

          J 1 Reply Last reply 23 Sept 2020, 15:52
          1
          • G Gojir4
            23 Sept 2020, 15:42

            @JonB Exact
            @blossomsg That's only for my editor knowing the type and providing auto-completion, sry for confusion

            J Offline
            J Offline
            JonB
            wrote on 23 Sept 2020, 15:52 last edited by
            #10

            @Gojir4
            No need to apologise, it's good style. The last time I first looked at Python at Python 3.something-less-than-7/6/5??, it had the : type for method parameters, and the -> type for returns, but not for variables, and it barfed if you tried one. So I wrote all my code using method param types (which personally I highly recommend, but then I like C++ and not Python!), and never could do anything about variables. So that support must have arrived at some more recent Python 3 version? (Or, it's just possible that it was PyCharm which didn't yet understand it, but I thought I saw it only arrived in a recent Python 3?)

            1 Reply Last reply
            1
            • S Offline
              S Offline
              SGaist
              Lifetime Qt Champion
              wrote on 23 Sept 2020, 17:51 last edited by
              #11

              Hi,

              Type hinting has been introduced in Python 3.5.

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              J 1 Reply Last reply 23 Sept 2020, 17:59
              1
              • S SGaist
                23 Sept 2020, 17:51

                Hi,

                Type hinting has been introduced in Python 3.5.

                J Offline
                J Offline
                JonB
                wrote on 23 Sept 2020, 17:59 last edited by JonB
                #12

                @SGaist
                Indeed. In 3.5 "type annotations of function parameters, a.k.a. type hints". But the line in @Gojir4 code:

                selectionModel: QItemSelectionModel = tree.selectionModel()
                

                is variable type-hinting. And I believe that did not come till Python 3.6, see https://stackoverflow.com/questions/39971929/what-are-variable-annotations-in-python-3-6 and https://docs.python.org/3.6/whatsnew/3.6.html#whatsnew36-pep526.

                So I must have started out on 3.5.x.

                S G 2 Replies Last reply 23 Sept 2020, 18:05
                1
                • J JonB
                  23 Sept 2020, 17:59

                  @SGaist
                  Indeed. In 3.5 "type annotations of function parameters, a.k.a. type hints". But the line in @Gojir4 code:

                  selectionModel: QItemSelectionModel = tree.selectionModel()
                  

                  is variable type-hinting. And I believe that did not come till Python 3.6, see https://stackoverflow.com/questions/39971929/what-are-variable-annotations-in-python-3-6 and https://docs.python.org/3.6/whatsnew/3.6.html#whatsnew36-pep526.

                  So I must have started out on 3.5.x.

                  S Offline
                  S Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on 23 Sept 2020, 18:05 last edited by
                  #13

                  @JonB You're right. I just read the question as "when did type hinting" come to Python.

                  Interested in AI ? www.idiap.ch
                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                  1 Reply Last reply
                  2
                  • J JonB
                    23 Sept 2020, 17:59

                    @SGaist
                    Indeed. In 3.5 "type annotations of function parameters, a.k.a. type hints". But the line in @Gojir4 code:

                    selectionModel: QItemSelectionModel = tree.selectionModel()
                    

                    is variable type-hinting. And I believe that did not come till Python 3.6, see https://stackoverflow.com/questions/39971929/what-are-variable-annotations-in-python-3-6 and https://docs.python.org/3.6/whatsnew/3.6.html#whatsnew36-pep526.

                    So I must have started out on 3.5.x.

                    G Offline
                    G Offline
                    Gojir4
                    wrote on 24 Sept 2020, 09:43 last edited by
                    #14

                    @JonB I did start using Python 3.6 and switched quickly to 3.8. Taking inspiration from PySide2 functions declaration it was natural for me to use this type hinting also in the code, without even knowing it was something new :). I agree with your I also use method param types, that's make code more clear and understandable, especially with custom types, and probably also because of ten years of C++ :D.

                    1 Reply Last reply
                    1
                    • B Offline
                      B Offline
                      blossomsg
                      wrote on 24 Sept 2020, 13:20 last edited by
                      #15

                      Hello Guys,

                      Thank you for all your effort especially @Gojir4
                      your snippet was amazing

                      https://stackoverflow.com/questions/21684336/how-to-get-the-index-for-the-filepath-of-a-selected-file-via-qfilesystemmodel

                      and the above link helped me resolve the 4 modelindex values to 1 modelindex --- which results 1 path rather than showing all the 4 column paths

                      from PySide2 import QtCore
                      from PySide2 import QtWidgets
                      from PySide2 import QtGui
                      import sys
                      
                      
                      def selectionChanged(selected, deselected):    
                          index = qtreeview_files.currentIndex()
                          print model.filePath(index)
                      
                      
                      qtreeview_files = QtWidgets.QTreeView()
                      model = QtWidgets.QFileSystemModel()
                      model.setRootPath("")
                      qtreeview_files.setModel(model)
                      qtreeview_files.hideColumn(1)
                      qtreeview_files.hideColumn(2)
                      qtreeview_files.hideColumn(3)
                      qtreeview_files.setSelectionMode(QtWidgets.QAbstractItemView.ExtendedSelection)
                      selectionModel  = qtreeview_files.selectionModel()
                      win_wid = QtWidgets.QWidget()
                      layout = QtWidgets.QVBoxLayout()
                      layout.addWidget(qtreeview_files)
                      win_wid.setLayout(layout)
                      #win_wid.resize(200, 50)
                      
                      
                      win_wid.show()
                      
                      
                      
                      selectionModel.selectionChanged.connect(selectionChanged)
                      
                      1 Reply Last reply
                      1
                      • B Offline
                        B Offline
                        blossomsg
                        wrote on 5 Oct 2020, 04:12 last edited by
                        #16

                        Sorry for late reply got caught up with some personal work

                        just an updated code.

                        from PySide2 import QtCore
                        from PySide2 import QtWidgets
                        from PySide2 import QtGui
                        import sys
                        import os
                        
                        
                        def selectionChanged(selected, deselected):    
                            index = qtreeview_files.currentIndex()
                            print model.filePath(index)
                        
                        
                        qtreeview_files = QtWidgets.QTreeView()
                        model = QtWidgets.QFileSystemModel()
                        model.setRootPath("")
                        qtreeview_files.setModel(model)
                        qtreeview_files.hideColumn(1)
                        qtreeview_files.hideColumn(2)
                        qtreeview_files.hideColumn(3)
                        qtreeview_files.setSelectionMode(QtWidgets.QAbstractItemView.ExtendedSelection)
                        selectionModel  = qtreeview_files.selectionModel()
                        win_wid = QtWidgets.QWidget()
                        layout = QtWidgets.QVBoxLayout()
                        layout.addWidget(qtreeview_files)
                        win_wid.setLayout(layout)
                        #win_wid.resize(200, 50)
                        
                        
                        win_wid.show()
                        
                        ##after you confirm the selection run this to get final selection list
                        list_of_sel_files = []
                        all = qtreeview_files.selectedIndexes()
                        for x in all:
                            print model.filePath(x)
                            list_of_sel_files.append(model.filePath(x))
                        ##########################################
                        
                        selectionModel.selectionChanged.connect(selectionChanged)
                        

                        closing ticket

                        1 Reply Last reply
                        1

                        • Login

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