PDA

View Full Version : Add additional information to QFileSystemModel



janck
17th June 2013, 17:48
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! :)

anda_skoa
18th June 2013, 09:20
Do I need to subclass QFileSystemModel and add additional data to it?

You can do that or use a QIdentityProxyModel



How to trim new and empty lines in plainTextEditor which is used for adding additional data?


QString::trimmed() should help with that.



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,
_

janck
18th June 2013, 12:21
You can do that or use a QIdentityProxyModel


How and where can I use it?



QString::trimmed() should help with that.


That's the way I've used it:


return ui->plainTextEdit->document()->toPlainText().trimmed();


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



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?

anda_skoa
18th June 2013, 18:01
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.




That's the way I've used it:


return ui->plainTextEdit->document()->toPlainText().trimmed();


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


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.


How to save other things from more textEdits?

Using QFile and either some format or QDataStream

Cheers,
_

janck
19th June 2013, 00:09
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?


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.

anda_skoa
19th June 2013, 07:53
New roles are variables?


Numerical values, see the role argument of QAbstractItemModel::data()
Usually via enum values, see Qt::ItemDataRoles



What is proxy subclass?

In this context a class derived from QIdentityProxyModel



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,
_

ChrisW67
19th June 2013, 09:02
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.

janck
19th June 2013, 09:36
@ChrisW67: Is the way to add additional information same as with QFileSystemModel? Is there any example how to implement this with QStringList?

ChrisW67
19th June 2013, 09:53
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.

janck
19th June 2013, 10:47
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.

ChrisW67
19th June 2013, 23:26
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().



#include <QApplication>
#include <QStandardItemModel>
#include <QListView>
#include <QStyle>
#include <QIcon>
#include <QDir>
#include <QFileInfoList>

int main(int argc, char **argv)
{
QApplication app(argc, argv);

const QIcon fileIcon = qApp->style()->standardPixmap(QStyle::SP_FileIcon);
const QIcon dirIcon = qApp->style()->standardPixmap(QStyle::SP_DirIcon);

QStandardItemModel model(0, 1);
QFileInfoList files = QDir().entryInfoList();
foreach(const QFileInfo &fi, files) {
QStandardItem *item = new QStandardItem(QString("%0").arg(fi.fileName()));
item->setFlags(Qt::ItemIsEnabled | Qt::ItemIsSelectable);
if (fi.isDir())
item->setData(dirIcon, Qt::DecorationRole);
else
item->setData(fileIcon, Qt::DecorationRole);
model.appendRow(item);
}

QListView view;
view.setModel(&model);
view.show();

return app.exec();
}

janck
20th June 2013, 11:56
QStandardItem *item = new QStandardItem(QString("%0").arg(fi.fileName()));
How can I access to this QString where fileName is stored?
EDIT: Answer:

model->itemFromIndex(index)->text();

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?