I am a beginner with the QFileSystemModel class and still trying to figure it out. I would like to create a custom file browser. I dug up enough code online to find out how to populate a QTableView with the folder contents of a specified root path. However, I was wondering how I might be able to insert additional rows of data into this model (something other than the files and folders that are already listed there). These rows could contain random words, icons, etc. Is this possible?
The only potential method I saw described in the docs is QFileSystemModel's .setData(). But I am not sure how to use it. It looks like I need to pass a few arguments into it in order to use it: QModelIndex , a QVariant value, and a role. Are all of these arguments mandatory? I don't completely understand the QModelIndex class and how to use it. Is this class always needed in order to get and set table data? I have used QTableWidgets before, but I would use QTableWidgetItem's for getting and setting. Does QModelIndex essentially play the same role as QTableWidgetItem?
Could anyone provide some example code as to how to insert a row of arbitrary text, etc into QFileSystemModel?
This is the code I have to so far for creating the model and table. It works fine, and displays all of the files and folders in specified directory. (it's in PyQt....but I certainly don't mind any C++ examples) Thanks!!
def __init__(self, *args):
super(Main, self).__init__(*args)
tableView.setSortingEnabled (True)
model = QtGui.QFileSystemModel()
tableView.setModel(model)
layout.addWidget(tableView)
self.setLayout(layout)
dir
= QtCore.
QDir('/user/bobby') root = model.setRootPath(dir.path())
files = dir.entryList()
tableView.setRootIndex(root)
class Main(QtGui.QWidget):
def __init__(self, *args):
super(Main, self).__init__(*args)
layout = QtGui.QHBoxLayout()
tableView = QtGui.QTableView()
tableView.setSortingEnabled (True)
model = QtGui.QFileSystemModel()
tableView.setModel(model)
layout.addWidget(tableView)
self.setLayout(layout)
dir = QtCore.QDir('/user/bobby')
root = model.setRootPath(dir.path())
files = dir.entryList()
tableView.setRootIndex(root)
To copy to clipboard, switch view to plain text mode
Bookmarks