View Full Version : QFileSystemModel: creating custom rows of data

15th December 2009, 04:49
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!!

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()




dir = QtCore.QDir('/user/bobby')
root = model.setRootPath(dir.path())
files = dir.entryList()

15th December 2009, 08:23
Create a proxy, set the source data to your instance of QFileSystemModel and do you modifications in the proxy.

16th December 2009, 02:25
Thanks for the info. I'm completely ignorant of proxy's, so I've got some researching to do :)

12th August 2013, 11:58

I also want to add custom row to QFileSystemModel. This newly added row will be having only some string e.g. "MyFiles" and when I click on this row, it will show files in some predefined folder.
Below is the non working code where I am trying to add row to existing QFileSystemModel:

I would appreciate any help with example , thanks.

MyFileSystemModel::MyFileSystemModel(QObject *parent)
: QFileSystemModel(parent)
//Lets wait to get model filled

void MyFileSystemModel::addFirstRow()
QString local("LOCAL");