Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. Making files in a directory into selectable items in a list view
QtWS25 Last Chance

Making files in a directory into selectable items in a list view

Scheduled Pinned Locked Moved Solved Qt for Python
5 Posts 3 Posters 1.5k 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.
  • X Offline
    X Offline
    xampierre
    wrote on last edited by
    #1

    Hi.

    I want to take all files in a directory(whose number can vary and i have no a priori info about its contents) and make them into a list of items that i can select(the classic scrollable list where a selected item has a blue background).

    I dont want to make them buttons because i want actions to be determined by pressing a generic 'Run' button. Also, i need to obtain the text written on the selected item so i can pass it as the path of that file.

    I tried using adding QStandardItems into a QStandardItemModel that was set as the widget for a QListView and it works fine, but i realized i cannot get the text written on the label.

    My code so far inside a basic main window:

    self.listView = QtWidgets.QListView()
    self.listModel = QtGui.QStandardItemModel()
    self.listView.setModel(self.listModel)
    ListAvailableMonths(self.listModel)
    self.monthList.setWidget(self.listView)
    
    def ListAvailableMonths(model):
        available = os.listdir('my file path')
    
        for month in available:
            item = QtGui.QStandardItem(month)
            model.appendRow(item)
    

    How can I solve the issue of not being able to get the item text?

    1 Reply Last reply
    0
    • X xampierre

      @SGaist The label i was talking about is the month in for month in available:. I want that when i press 'Run' with a selected item in that list, the text on that item, namely the name of that month, gets sent to a method.

      WS0nsjo.png

      In this example, i would like that when pressing ok, 'List Item 13' is obtained as a text so i can make it into a path.

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by JonB
      #4

      @xampierre
      void QAbstractItemView::clicked(const QModelIndex &index) gives you the index into the model of the item clicked. Then calling index.data() will give you the item data, i.e. the string.

      If you want to do it when pressing the OK button, you need to need to know the QListView()'s selected item. That can be obtained from QModelIndexList QListView::selectedIndexes() const.

      X 1 Reply Last reply
      2
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #2

        Hi,

        What label are you talking about ?
        How are you trying to access the model data ?
        On a related note, did you consider using QFileSystemModel ?

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

        X 1 Reply Last reply
        0
        • SGaistS SGaist

          Hi,

          What label are you talking about ?
          How are you trying to access the model data ?
          On a related note, did you consider using QFileSystemModel ?

          X Offline
          X Offline
          xampierre
          wrote on last edited by
          #3

          @SGaist The label i was talking about is the month in for month in available:. I want that when i press 'Run' with a selected item in that list, the text on that item, namely the name of that month, gets sent to a method.

          WS0nsjo.png

          In this example, i would like that when pressing ok, 'List Item 13' is obtained as a text so i can make it into a path.

          JonBJ 1 Reply Last reply
          0
          • X xampierre

            @SGaist The label i was talking about is the month in for month in available:. I want that when i press 'Run' with a selected item in that list, the text on that item, namely the name of that month, gets sent to a method.

            WS0nsjo.png

            In this example, i would like that when pressing ok, 'List Item 13' is obtained as a text so i can make it into a path.

            JonBJ Offline
            JonBJ Offline
            JonB
            wrote on last edited by JonB
            #4

            @xampierre
            void QAbstractItemView::clicked(const QModelIndex &index) gives you the index into the model of the item clicked. Then calling index.data() will give you the item data, i.e. the string.

            If you want to do it when pressing the OK button, you need to need to know the QListView()'s selected item. That can be obtained from QModelIndexList QListView::selectedIndexes() const.

            X 1 Reply Last reply
            2
            • JonBJ JonB

              @xampierre
              void QAbstractItemView::clicked(const QModelIndex &index) gives you the index into the model of the item clicked. Then calling index.data() will give you the item data, i.e. the string.

              If you want to do it when pressing the OK button, you need to need to know the QListView()'s selected item. That can be obtained from QModelIndexList QListView::selectedIndexes() const.

              X Offline
              X Offline
              xampierre
              wrote on last edited by
              #5

              @JonB Thanks a lot, exactly what i was looking for.

              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