PDA

View Full Version : QFileSystemModel: creating custom rows of data



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

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)

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

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

alfaalfa
12th August 2013, 11:58
Hi,

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)
{
this->setRootPath("C:/");
//Lets wait to get model filled
QTimer::singleShot(1000,this,SLOT(addFirstRow()));
}

void MyFileSystemModel::addFirstRow()
{
QString local("LOCAL");
this->insertRow(0);
this->setData(this->index(0,0),local,Qt::DisplayRole);
}