Results 1 to 2 of 2

Thread: QListView and QStringListModel

  1. #1
    Join Date
    Nov 2007
    Qt products
    Unix/X11 Windows

    Default QListView and QStringListModel

    Good evening,
    I am developing an application where I have to process a bunch of image files.
    To load the file's list, the user have to press a load button and a QFileDialog is created, then I populate a QStringList model with the file's paths.
    To show the list I use a QListView
    Here the code:

    Qt Code:
    1. QFileDialog fileDialog;
    2. fileDialog.setWindowTitle(tr("Open Directory"));
    3. fileDialog.setFileMode(QFileDialog::DirectoryOnly);
    5. if (fileDialog.exec() == QDialog::Accepted)
    6. {
    7. QStringList files = getImageFiles(fileDialog.selectedFiles().first());
    9. // Populate the model
    10. m_model->setStringList(files);
    12. // and pass it to the view
    13. ui->listView->setModel(m_model);
    14. }
    16. QStringList MainWindow::getImageFiles(const QString& path) const
    17. {
    18. QStringList result;
    19. QDir root(path);
    21. QStringList dirs = root.entryList(QDir::AllDirs | QDir::NoDotAndDotDot, QDir::Name);
    23. foreach (const QString& dir, dirs)
    24. {
    25. result.append(getImageFiles(root.filePath(dir)));
    26. }
    28. QStringList files = root.entryList(QString("*.jpg").split(";"), QDir::Files, QDir::Name);
    30. foreach (const QString& file, files)
    31. {
    32. result.append(root.filePath(file));
    33. }
    35. return result;
    36. }
    To copy to clipboard, switch view to plain text mode 

    Now in the view I have a list of full paths, but I would only show the files names instead of full path while keeping the full paths in the model
    How can I do it? I would have an help on it.
    Thanx in advance
    Franco Amato

  2. #2
    Join Date
    Jan 2008
    Alameda, CA, USA
    Thanked 677 Times in 667 Posts
    Qt products
    Windows Android

    Default Re: QListView and QStringListModel

    How can I do it? I would have an help on it.
    Derive a custom model from QStringListModel, and override the data() method. In the handler for DisplayRole, use QFileInfo to retrieve the file name only from the path (using QFileInfo::fileName()) and return that as the QVariant. For all other data roles, call the QStringListModel base class data() method.

    For your processing where you need the entire path, you can implement a handler for Qt::UserRole in your custom model. For that role, you call the base class data() method with the Qt::DisplayRole role, which will return the entire path string. In your list-handling code, you would call into the model with the UserRole when you need the full path.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

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

    franco.amato (7th December 2019)

Similar Threads

  1. QML Combobox with QStringListModel
    By volcano in forum Qt Quick
    Replies: 3
    Last Post: 29th November 2016, 07:59
  2. Replies: 1
    Last Post: 3rd September 2013, 20:47
  3. Replies: 4
    Last Post: 7th March 2013, 17:20
  4. QListView
    By Yayati.Ekbote in forum Qt Programming
    Replies: 1
    Last Post: 23rd January 2010, 19:50
  5. QListView
    By moowy in forum Qt Programming
    Replies: 2
    Last Post: 2nd October 2006, 15:14


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.