Results 1 to 4 of 4

Thread: QFileSystemModel: creating custom rows of data

  1. #1
    Join Date
    Sep 2009
    Posts
    4
    Thanks
    2
    Qt products
    Platforms
    Unix/X11 Windows

    Default QFileSystemModel: creating custom rows of data

    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!!


    Qt Code:
    1. class Main(QtGui.QWidget):
    2. def __init__(self, *args):
    3. super(Main, self).__init__(*args)
    4.  
    5. layout = QtGui.QHBoxLayout()
    6.  
    7. tableView = QtGui.QTableView()
    8. tableView.setSortingEnabled (True)
    9. model = QtGui.QFileSystemModel()
    10.  
    11. tableView.setModel(model)
    12.  
    13. layout.addWidget(tableView)
    14.  
    15.  
    16. self.setLayout(layout)
    17.  
    18. dir = QtCore.QDir('/user/bobby')
    19. root = model.setRootPath(dir.path())
    20. files = dir.entryList()
    21. tableView.setRootIndex(root)
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QFileSystemModel: creating custom rows of data

    Create a proxy, set the source data to your instance of QFileSystemModel and do you modifications in the proxy.

  3. #3
    Join Date
    Sep 2009
    Posts
    4
    Thanks
    2
    Qt products
    Platforms
    Unix/X11 Windows

    Default Re: QFileSystemModel: creating custom rows of data

    Thanks for the info. I'm completely ignorant of proxy's, so I've got some researching to do

  4. #4
    Join Date
    Jun 2013
    Posts
    6
    Thanks
    2
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: QFileSystemModel: creating custom rows of data

    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);
    }

Similar Threads

  1. Get custom data from qstandarditem?
    By alexandernst in forum Newbie
    Replies: 3
    Last Post: 11th August 2009, 04:24
  2. Custom Model Advice Requested
    By mclark in forum Qt Programming
    Replies: 3
    Last Post: 18th September 2008, 17:26
  3. Creating a QImage from uchar* data
    By forrestfsu in forum Qt Programming
    Replies: 6
    Last Post: 8th February 2007, 16:21
  4. QVariant::toString and custom data types
    By Vladimir in forum Qt Programming
    Replies: 2
    Last Post: 16th January 2007, 16:36
  5. Replies: 16
    Last Post: 7th March 2006, 16:57

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.