Results 1 to 12 of 12

Thread: Add additional information to QFileSystemModel

  1. #1
    Join Date
    Jun 2013
    Posts
    22
    Thanks
    1
    Qt products
    Qt5

    Default Add additional information to QFileSystemModel

    Hello!

    I would like to save some additional data to QFileSystemModel as is file description and things like that. I already have an output window in which the user can enter some additional, not required data, but anyway I need to implement this if the users would like to add anything else than the main app allows.

    Do I need to subclass QFileSystemModel and add additional data to it?
    How to trim new and empty lines in plainTextEditor which is used for adding additional data?
    How to output the data of QFileSystem to a file and then to import it back?

    Thank you for answers!

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Add additional information to QFileSystemModel

    Quote Originally Posted by janck View Post
    Do I need to subclass QFileSystemModel and add additional data to it?
    You can do that or use a QIdentityProxyModel

    Quote Originally Posted by janck View Post
    How to trim new and empty lines in plainTextEditor which is used for adding additional data?
    QString::trimmed() should help with that.

    Quote Originally Posted by janck View Post
    How to output the data of QFileSystem to a file and then to import it back?
    What do you mean? QFileSystem model shows the file system, that data is on disk.

    Cheers,
    _

  3. #3
    Join Date
    Jun 2013
    Posts
    22
    Thanks
    1
    Qt products
    Qt5

    Default Re: Add additional information to QFileSystemModel

    Quote Originally Posted by anda_skoa View Post
    You can do that or use a QIdentityProxyModel
    How and where can I use it?

    Quote Originally Posted by anda_skoa View Post
    QString::trimmed() should help with that.
    That's the way I've used it:
    Qt Code:
    1. return ui->plainTextEdit->document()->toPlainText().trimmed();
    To copy to clipboard, switch view to plain text mode 

    But it doesn't work. If I enter more lines and spaces between some text I get the exactly same

    Quote Originally Posted by anda_skoa View Post
    What do you mean? QFileSystem model shows the file system, that data is on disk.
    Okay, so I need to save just a directory path in file <- I can't do only that because I have an option to delete files from list, so I need to save filenames into output file (the program shouldn't load files which have been already deleted). How to save other things from more textEdits?
    Last edited by janck; 18th June 2013 at 12:25.

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Add additional information to QFileSystemModel

    Quote Originally Posted by janck View Post
    How and where can I use it?
    You define some new roles and deliver the respective data in your subclass or in the proxy subclass.


    Quote Originally Posted by janck View Post
    That's the way I've used it:
    Qt Code:
    1. return ui->plainTextEdit->document()->toPlainText().trimmed();
    To copy to clipboard, switch view to plain text mode 

    But it doesn't work. If I enter more lines and spaces between some text I get the exactly same
    Spaces inbetween can be removed by simplified()

    Quote Originally Posted by janck View Post
    I can't do only that because I have an option to delete files from list, so I need to save filenames into output file (the program shouldn't load files which have been already deleted).
    If you delete files they shouldn't be displayed in the file system model anymore. After all the model is a representation of the file system.

    Quote Originally Posted by janck View Post
    How to save other things from more textEdits?
    Using QFile and either some format or QDataStream

    Cheers,
    _

  5. #5
    Join Date
    Jun 2013
    Posts
    22
    Thanks
    1
    Qt products
    Qt5

    Default Re: Add additional information to QFileSystemModel

    Quote Originally Posted by anda_skoa View Post
    You define some new roles and deliver the respective data in your subclass or in the proxy subclass.
    New roles are variables? What is proxy subclass?

    Quote Originally Posted by anda_skoa View Post
    If you delete files they shouldn't be displayed in the file system model anymore. After all the model is a representation of the file system.
    I don't want to delete files from disk, but only from listView in application. Remaining items on list would be loaded after project opening and deleted shouldn't.

  6. #6
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Add additional information to QFileSystemModel

    Quote Originally Posted by janck View Post
    New roles are variables?
    Numerical values, see the role argument of QAbstractItemModel::data()
    Usually via enum values, see Qt::ItemDataRoles

    Quote Originally Posted by janck View Post
    What is proxy subclass?
    In this context a class derived from QIdentityProxyModel

    Quote Originally Posted by janck View Post
    I don't want to delete files from disk, but only from listView in application. Remaining items on list would be loaded after project opening and deleted shouldn't.
    Ah, then you need to filter out the respective roles. E.g. by using QSortFilterProxyModel

    Cheers,
    _

  7. #7
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Add additional information to QFileSystemModel

    I don't want to delete files from disk, but only from listView in application. Remaining items on list would be loaded after project opening and deleted shouldn't.
    Then you should not be using a QFileSystemModel. What you have is a list of arbitrary strings that only start out life matching the actual files on the file system but the moment the user edits the list the alignment is lost. You should perhaps use a QStringListModel that you initially populate using QDir but thereafter maintain separately.

  8. #8
    Join Date
    Jun 2013
    Posts
    22
    Thanks
    1
    Qt products
    Qt5

    Default Re: Add additional information to QFileSystemModel

    @ChrisW67: Is the way to add additional information same as with QFileSystemModel? Is there any example how to implement this with QStringList?

  9. #9
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Add additional information to QFileSystemModel

    QStringListModel is a QAbstractItemModel just like QFileSystemModel so the same approach applies: either add data in a user roles/roles, or add columns to a table or tree.

    What additional information and how were you intending to display it? You have a simple list view, so there's only one column.

  10. #10
    Join Date
    Jun 2013
    Posts
    22
    Thanks
    1
    Qt products
    Qt5

    Default Re: Add additional information to QFileSystemModel

    Now is problem with display of items in listView. When I was using QFileSystemModel there were no problem with dobule clicking, but now when I double click on the item, there is edit mode. How to prevent this, because on dobule click I want to open dialog in which is description and few other things? Also with this problem, icons disappeared - is possible to show some file icon before file name?

    Description of each file would be showed in dialog and should be saved on clicking button OK.

  11. #11
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Add additional information to QFileSystemModel

    Have you read Model/View Programming and Model/View Tutorial?

    Yes, it is possible to have an icon displayed with an entry in a model and control editing but not with QStringListModel (which presents an editable list of strings for things like combobox drop downs). To gain finer control you can use QStandardItemModel. The icon displayed is the value returned when QAbstractItemModel::data() is called with the role Qt::DecorationRole... by default null pixmap. Editability is controlled by the flags().

    Qt Code:
    1. #include <QApplication>
    2. #include <QStandardItemModel>
    3. #include <QListView>
    4. #include <QStyle>
    5. #include <QIcon>
    6. #include <QDir>
    7. #include <QFileInfoList>
    8.  
    9. int main(int argc, char **argv)
    10. {
    11. QApplication app(argc, argv);
    12.  
    13. const QIcon fileIcon = qApp->style()->standardPixmap(QStyle::SP_FileIcon);
    14. const QIcon dirIcon = qApp->style()->standardPixmap(QStyle::SP_DirIcon);
    15.  
    16. QStandardItemModel model(0, 1);
    17. QFileInfoList files = QDir().entryInfoList();
    18. foreach(const QFileInfo &fi, files) {
    19. QStandardItem *item = new QStandardItem(QString("%0").arg(fi.fileName()));
    20. item->setFlags(Qt::ItemIsEnabled | Qt::ItemIsSelectable);
    21. if (fi.isDir())
    22. item->setData(dirIcon, Qt::DecorationRole);
    23. else
    24. item->setData(fileIcon, Qt::DecorationRole);
    25. model.appendRow(item);
    26. }
    27.  
    28. QListView view;
    29. view.setModel(&model);
    30. view.show();
    31.  
    32. return app.exec();
    33. }
    To copy to clipboard, switch view to plain text mode 

  12. #12
    Join Date
    Jun 2013
    Posts
    22
    Thanks
    1
    Qt products
    Qt5

    Default Re: Add additional information to QFileSystemModel

    Qt Code:
    1. QStandardItem *item = new QStandardItem(QString("%0").arg(fi.fileName()));
    To copy to clipboard, switch view to plain text mode 
    How can I access to this QString where fileName is stored?
    EDIT: Answer:
    Qt Code:
    1. model->itemFromIndex(index)->text();
    To copy to clipboard, switch view to plain text mode 

    In my listView are only files, because I filter out only those which have certain extension. Is with "item->setData()" set just icon and is it there any other way to set the fileName & icon to all files without loop?
    Last edited by janck; 20th June 2013 at 17:19.

Similar Threads

  1. Replies: 4
    Last Post: 20th April 2011, 12:15
  2. Replies: 0
    Last Post: 23rd February 2011, 14:24
  3. XCode and additional files
    By themolecule in forum Qt Programming
    Replies: 0
    Last Post: 6th May 2008, 06:57
  4. Additional QtMsgHandler information
    By bruccutler in forum Newbie
    Replies: 2
    Last Post: 23rd April 2007, 16:38
  5. QDate without additional character
    By raphaelf in forum Qt Programming
    Replies: 2
    Last Post: 17th February 2006, 21:16

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.