Results 1 to 15 of 15

Thread: Save the contents of a listbox with filePath of currentIndex but display fileName

  1. #1
    Join Date
    Nov 2012
    Posts
    23
    Thanks
    9
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Symbian S60

    Default Save the contents of a listbox with filePath of currentIndex but display fileName

    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.

  2. #2
    Join Date
    Jan 2009
    Location
    The Netherlands and Spain
    Posts
    150
    Thanks
    6
    Thanked 18 Times in 18 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Save the contents of a listbox with filePath of currentIndex but display fileName

    Try this:
    Qt Code:
    1. QListWidgetItem *newItem;
    2. newItem = new QListWidgetItem(fileName);
    3. newItem->setData(Qt::DisplayRole, filePath);
    4. ui->listWidget.addItem(newItem);
    To copy to clipboard, switch view to plain text mode 

  3. The following user says thank you to boudie for this useful post:

    sargeslash (16th January 2013)

  4. #3
    Join Date
    Nov 2012
    Posts
    23
    Thanks
    9
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Symbian S60

    Default Re: Save the contents of a listbox with filePath of currentIndex but display fileName

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

  5. #4
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: Save the contents of a listbox with filePath of currentIndex but display fileName

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

    Qt Code:
    1. #include <QApplication>
    2. #include <QtGui>
    3. #include <QtCore>
    4.  
    5. class Worker : public QObject
    6. {
    7. Q_OBJECT
    8. public:
    9. explicit Worker(QListWidget *parent)
    10. : QObject(parent)
    11. , list(parent)
    12. {
    13. list->setWindowTitle("QListWidget");
    14. }
    15.  
    16. public slots:
    17. void updateList(void)
    18. {
    19. QFileSystemModel *model = dynamic_cast<QFileSystemModel *>(sender());
    20. if(model != 0)
    21. {
    22. list->clear();
    23. QModelIndex root = model->index(0, 0);
    24. for(int i = 0; i < model->rowCount(root); i++)
    25. {
    26. QModelIndex index = model->index(i, 0, root);
    27. item = new QListWidgetItem;
    28. item->setData(Qt::DisplayRole, model->fileName(index));
    29. item->setData(Qt::UserRole, model->filePath(index));
    30. list->addItem(item);
    31. }
    32. list->show();
    33. }
    34. }
    35.  
    36. void saveList(void)
    37. {
    38. QFile file("D:/FileList.csv");
    39. if(file.open(QFile::WriteOnly | QFile::Text))
    40. {
    41. for(int i = 0; i < list->count(); i++)
    42. {
    43. file.write(list->model()->index(i, 0).data(Qt::UserRole).toString().toStdString().c_str());
    44. file.write("\n");
    45. }
    46. file.close();
    47. }
    48. }
    49.  
    50. private:
    51. QListWidget *list;
    52. };
    53.  
    54. int main(int argc, char *argv[])
    55. {
    56. QApplication app(argc, argv);
    57. QTreeView view;
    58. QFileSystemModel *model = new QFileSystemModel(&view);
    59.  
    60. view.setModel(model);
    61. view.setRootIndex(model->index("D:/"));
    62. view.show();
    63.  
    64. Worker *worker = new Worker(&list);
    65. QObject::connect(model, SIGNAL(directoryLoaded(QString)), worker, SLOT(updateList()));
    66. QObject::connect(model, SIGNAL(directoryLoaded(QString)), worker, SLOT(saveList()));
    67.  
    68. model->setRootPath("D:/");
    69. return app.exec();
    70. }
    71.  
    72. #include "Main.moc"
    To copy to clipboard, switch view to plain text mode 
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  6. The following user says thank you to Santosh Reddy for this useful post:

    sargeslash (17th January 2013)

  7. #5
    Join Date
    Nov 2012
    Posts
    23
    Thanks
    9
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Symbian S60

    Default Re: Save the contents of a listbox with filePath of currentIndex but display fileName

    Thanks a lot my problem is solved
    qtcentre is so awesome

  8. #6
    Join Date
    Nov 2012
    Posts
    23
    Thanks
    9
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Symbian S60

    Default Re: Save the contents of a listbox with filePath of currentIndex but display fileName

    I am facing seg fault when I am using the filePath

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

    when I am adding the file the applicaiton is crashing

  9. #7
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: Save the contents of a listbox with filePath of currentIndex but display fileName

    are you sure? can you post a compilable example with this problem
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  10. #8
    Join Date
    Nov 2012
    Posts
    23
    Thanks
    9
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Symbian S60

    Default Re: Save the contents of a listbox with filePath of currentIndex but display fileName

    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, SLOT(addImgFiles()));


    void MainWindow::addImgFiles()
    {
    if(ui->inputImageLstBox->count()<8)
    {
    QModelIndex index = ui->treeView->currentIndex();
    QListWidgetItem *item;
    item = new QListWidgetItem;
    item->setData(Qt:isplayRole, 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();
    }
    }

  11. #9
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: Save the contents of a listbox with filePath of currentIndex but display fileName

    I cannot compile the code
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  12. #10
    Join Date
    Nov 2012
    Posts
    23
    Thanks
    9
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Symbian S60

    Default Re: Save the contents of a listbox with filePath of currentIndex but display fileName

    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.

  13. #11
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: Save the contents of a listbox with filePath of currentIndex but display fileName

    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
    Qt Code:
    1. if(ui->treeView->currentIndex().isValid())
    2. {
    3. item->setData(Qt::UserRole, model->filePath(ui->treeView->currentIndex()));
    4. }
    To copy to clipboard, switch view to plain text mode 
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  14. #12
    Join Date
    Nov 2012
    Posts
    23
    Thanks
    9
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Symbian S60

    Default Re: Save the contents of a listbox with filePath of currentIndex but display fileName

    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.

  15. #13
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: Save the contents of a listbox with filePath of currentIndex but display fileName

    Please answer this......Where and when are you calling.....?
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  16. #14
    Join Date
    Nov 2012
    Posts
    23
    Thanks
    9
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Symbian S60

    Default Re: Save the contents of a listbox with filePath of currentIndex but display fileName

    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.

  17. #15
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: Save the contents of a listbox with filePath of currentIndex but display fileName

    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
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  18. The following user says thank you to Santosh Reddy for this useful post:

    sargeslash (24th January 2013)

Similar Threads

  1. save contents QTextEdit in a .txt file
    By giorgik in forum Newbie
    Replies: 2
    Last Post: 31st May 2012, 10:11
  2. display filename
    By kamlmish in forum Newbie
    Replies: 2
    Last Post: 22nd December 2010, 04:00
  3. Replies: 3
    Last Post: 20th December 2010, 12:18
  4. QFileDialog for choosing filename to save data
    By araglin in forum Qt Programming
    Replies: 2
    Last Post: 1st April 2009, 08:46
  5. How to display listbox, texbox etc over an image.
    By mahe2310 in forum Qt Programming
    Replies: 12
    Last Post: 3rd March 2006, 15:51

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.