PDA

View Full Version : Reading PDFs from a directory



ayanda83
1st October 2016, 21:24
I am hoping to get some help here guys. I am developing this program that reads and displays pdf documents from a directory. The program does not actually open the pdf documents, it just displays them in list format in a QListView. My problem is that the program reads only the title/name of the pdf documents and leaves the icon next to the title. The picture on the left, shows a screenshot of how the program reads and displays the pdf documents (without the icons). The picture on the right is how the pdf documents are displayed in the directory and that is how I would like them to be displayed by my program. 1213512136

d_stranz
1st October 2016, 22:15
If you are feeding your QListView from a model derived from QAbstractItemModel, then you need to implement the Qt::DecorationRole inside your model's data() method (QAbstractItemModel::data()) and supply the appropriate QIcon. QListView doesn't know these are PDF document names. The directory view asks the OS for the icon associated with entries in a directory, so that's why you see them there.

It's hard to see because your screenshot is so tiny, but it also looks like your filenames are aligned horizontal center in each column. If you don't want that, then you also need to implement the Qt:: AlignmentRole in the data() method, and return Qt:: AlignLeft | Qt:: AlignVCenter.

ayanda83
1st October 2016, 23:13
The directory view asks the OS for the icon associated with entries in a directory, so that's why you see them there.
Is there no way I get my program to ask the OS for an associated icon to a file instead of supplying it myself?

d_stranz
1st October 2016, 23:31
Is there no way I get my program to ask the OS for an associated icon to a file instead of supplying it myself?

I looked for that as I was making my original answer. QFileSystemModel has a QFileSystemModel::FileIconRole that gets mapped to the DecorationRole. QFileDialog apparently knows to ask for this (along with some of the Qt:: UserRole extras that the file system model provides). I thought QFileInfo might serve up the icon, but it doesn't. I think you'll have to look into the QFileSystemModel source code to find out what it is doing.

You can download the official PDF icon from Adobe here (http://www.adobe.com/legal/permissions/icons-web-logos.html#pdfs).

Edit: Aha! There is a QFileIconProvider class!

ayanda83
2nd October 2016, 00:43
Thank you very much, you've been a great help. I'm going to check-out QFileIconProvider, I'm pretty sure this will solve my problem.