Making files in a directory into selectable items in a list view
-
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
QStandardItem
s into aQStandardItemModel
that was set as the widget for aQListView
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?
-
@SGaist The label i was talking about is the
month
infor 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.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.
@xampierre
void QAbstractItemView::clicked(const QModelIndex &index) gives you the index into the model of the item clicked. Then callingindex.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. -
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 ? -
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 ?@SGaist The label i was talking about is the
month
infor 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.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.
-
@SGaist The label i was talking about is the
month
infor 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.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.
@xampierre
void QAbstractItemView::clicked(const QModelIndex &index) gives you the index into the model of the item clicked. Then callingindex.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. -
@xampierre
void QAbstractItemView::clicked(const QModelIndex &index) gives you the index into the model of the item clicked. Then callingindex.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.