PDA

View Full Version : Save the contents of a listbox with filePath of currentIndex but display fileName



sargeslash
15th January 2013, 14:17
I have to save the contents of a listBox in a file, in this listBox I have data added from a QTreeView(with model QFileSystemModel)

QModelIndex index = ui->treeView->currentIndex()
QString pathToFile=model->filePath(index);
ui->inputImageLstBox->addItem(pathToFile);

So right now I am able to save contents of this listBox but I want to display the contents as QString filename=model->fileName(index);
in the listBox but save the data with FilePath and not the FileName.

I am sorry if I am not able to express myself properly.

boudie
16th January 2013, 00:31
Try this:


QListWidgetItem *newItem;
newItem = new QListWidgetItem(fileName);
newItem->setData(Qt::DisplayRole, filePath);
ui->listWidget.addItem(newItem);

sargeslash
16th January 2013, 14:39
I tried it but may be not rightly, can you clearify what should be fileName and filePath

QListWidgetItem *newItem;
QModelIndex index = ui->treeView->currentIndex();
QString fileName=model->fileName(index);
QString filePath=model->filePath(index);
newItem = new QListWidgetItem(fileName);
newItem->setData(Qt:: DisplayRole, filePath);
ui->inputImageLstBox->addItem(newItem);

I have implemented it like above but it is not working as the application is crashing, this is the output of the backtracking

Program received signal SIGSEGV, Segmentation fault.
0x00007ffff6d98466 in QFileSystemModel::filePath(QModelIndex const&) const ()

Santosh Reddy
16th January 2013, 16:23
One way is to store both filename, and filepath in the list item in different item roles. In the example below, filename is store in Qt::DisplayRole, and filepath is stored in Qt::UserRole. Qt::DisplayRole (filename) is used to display item in the list, and Qt::UserRole (filepath) can be used to save in to a file.

Have fun exploring how this program works, it works :)



#include <QApplication>
#include <QtGui>
#include <QtCore>

class Worker : public QObject
{
Q_OBJECT
public:
explicit Worker(QListWidget *parent)
: QObject(parent)
, list(parent)
{
list->setWindowTitle("QListWidget");
}

public slots:
void updateList(void)
{
QFileSystemModel *model = dynamic_cast<QFileSystemModel *>(sender());
QListWidgetItem *item;
if(model != 0)
{
list->clear();
QModelIndex root = model->index(0, 0);
for(int i = 0; i < model->rowCount(root); i++)
{
QModelIndex index = model->index(i, 0, root);
item = new QListWidgetItem;
item->setData(Qt::DisplayRole, model->fileName(index));
item->setData(Qt::UserRole, model->filePath(index));
list->addItem(item);
}
list->show();
}
}

void saveList(void)
{
QFile file("D:/FileList.csv");
if(file.open(QFile::WriteOnly | QFile::Text))
{
for(int i = 0; i < list->count(); i++)
{
file.write(list->model()->index(i, 0).data(Qt::UserRole).toString().toStdString().c_s tr());
file.write("\n");
}
file.close();
}
}

private:
QListWidget *list;
};

int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QTreeView view;
QFileSystemModel *model = new QFileSystemModel(&view);

view.setModel(model);
view.setRootIndex(model->index("D:/"));
view.show();

QListWidget list;
Worker *worker = new Worker(&list);
QObject::connect(model, SIGNAL(directoryLoaded(QString)), worker, SLOT(updateList()));
QObject::connect(model, SIGNAL(directoryLoaded(QString)), worker, SLOT(saveList()));

model->setRootPath("D:/");
return app.exec();
}

#include "Main.moc"

sargeslash
17th January 2013, 10:41
Thanks a lot my problem is solved
qtcentre is so awesome :)

sargeslash
23rd January 2013, 13:31
I am facing seg fault when I am using the filePath

QModelIndex index = ui->treeView->currentIndex();
QListWidgetItem *item;
item = new QListWidgetItem;
item->setData(Qt::DisplayRole, model->fileName(index));
item->setData(Qt::UserRole, model->filePath(index));
ui->inputImageLstBox->addItem(item);

when I am adding the file the applicaiton is crashing

Santosh Reddy
23rd January 2013, 14:13
are you sure? can you post a compilable example with this problem

sargeslash
23rd January 2013, 14:22
I am not sure why is this happening as it just occurred.
I am trying to add the file from the qTreeview to listbox using

connect(ui->treeView,SIGNAL(doubleClicked(QModelIndex)),this,S LOT(addImgFiles()));


void MainWindow::addImgFiles()
{
if(ui->inputImageLstBox->count()<8)
{
QModelIndex index = ui->treeView->currentIndex();
QListWidgetItem *item;
item = new QListWidgetItem;
item->setData(Qt::DisplayRole, model->fileName(index));
item->setData(Qt::UserRole, model->filePath(ui->treeView->currentIndex()));
}
else
ui->addFiletoListButton->setEnabled(false);
}


void MainWindow::saveInputImageList(void)
{
outputFilename = outputDirectory + "/output.lst";
QFile outputFile(outputFilename);
if(outputFile.open(QFile::WriteOnly | QFile::Text))
{
for(int i = 0; i < ui->inputImageLstBox->count(); i++)
{
outputFile.write(ui->inputImageLstBox->model()->index(i, 0).data(Qt::UserRole).toString().toStdString().c_s tr());
outputFile.write("\n");
}
outputFile.close();
}
}

Santosh Reddy
23rd January 2013, 14:39
I cannot compile the code :(

sargeslash
23rd January 2013, 14:46
This is the output of the backtrace

Program received signal SIGSEGV, Segmentation fault.
0x00007ffff6d98466 in QFileSystemModel::filePath(QModelIndex const&) const () from /usr/lib/x86_64-linux-gnu/libQtGui.so.4

Its only a part of the whole code so it will not compile but is there any precautions while using the filePath(index)?
As after commenting " item->setData(Qt::UserRole, model->filePath(ui->treeView->currentIndex())); "
I can run the program.

Santosh Reddy
23rd January 2013, 15:32
Where and when are you calling "item->setData(Qt::UserRole, model->filePath(ui->treeView->currentIndex()));",

Is the view populated when this executes?

Try doing it this way


if(ui->treeView->currentIndex().isValid())
{
item->setData(Qt::UserRole, model->filePath(ui->treeView->currentIndex()));
}

sargeslash
23rd January 2013, 15:51
I am using "item->setData(Qt::UserRole, model->filePath(ui->treeView->currentIndex()));" to save it in a file (i.e. Qt::UserRole)
I have set the model to the view in the constructor itself.

QFileSystemModel *model = new QFileSystemModel;
model->setRootPath("/home/");
ui->treeView->setModel(model);

I have tried the above code also but it failed also.

Santosh Reddy
23rd January 2013, 17:32
Please answer this......Where and when are you calling.....?

sargeslash
24th January 2013, 09:21
I am calling it in addImage function(function to addImages from QtreeView to QlistBox), when I am adding the currentIndex item from QtreeView to QlistBox. I am using it while saving the file in a text file.

Santosh Reddy
24th January 2013, 10:27
I have set the model to the view in the constructor itself.
You need to wait untill the file system model is loaded, and then in a slot connected to the load complete signal fileName()/filePath().

Refer to the example code posted earlier in the same thread, it does the same thing, it waits for the filesystem model to load and then loads the list box