PDA

View Full Version : Is it possible to add headers to a QListView?



Valheru
5th October 2006, 18:21
Possibly by subclassing a QAbstractListModel? AFAIK there is no way to set headers for a QListView, is there?

wysota
5th October 2006, 18:26
Why would you want to add a header if there is only one column in the view?

jacek
5th October 2006, 18:26
As the docs say:
This view does not display horizontal or vertical headers; to display a list of items with a horizontal header, use QTreeView instead.

Valheru
5th October 2006, 18:43
Why would you want to add a header if there is only one column in the view?

Well, for asthetic purposes mainly. But I didn't say that there would be only one column in the view :p There will be a between two and four, depending on the tab being viewed.

But anyway, it seems to be possible for a QAbstractListView, since it inherits from the QAbstractItemModel - and that has a headerData() method.

I ask this because in my program, updating a QSimpleItemModel using the setData() method takes about 20 seconds when the newsserver being listed has 176.000 groups :p Which freezes the GUI for about 20 seconds - which may be acceptable to whoever wrote Grabit, but it's exactly what I'm trying to avoid in my program. So I thought that by creating a simple read-only model, and by setting the model of the QListView to the newly created model, I could speed things up quite a bit. This is true, right? Calling setData() seems to have a lot of overhead with large amounts of rows, as searching this forum would have me believe ...

/edit : just implemented it - it works like jacek says with a QTreeView and a QAbstractItemModel. Bugger, I was trying to get away from those lines drawn down the side, since it looks ugly when they aren't really related to each other. But it still holds true for the speed issue.

jacek
5th October 2006, 18:59
I was trying to get away from those lines drawn down the side, since it looks ugly when they aren't really related to each other.
Try changing rootIsDecorated property to false or use QTableView.

Valheru
5th October 2006, 19:19
Thanks :) That gets rid of teh ugly at least :p Do you know if it will be fast to reassign a model subclassed from a QAbstractItemModel?

jacek
5th October 2006, 19:38
Do you know if it will be fast to reassign a model subclassed from a QAbstractItemModel?
No, I don't. I've never tried to do such benchmarks.

wysota
5th October 2006, 20:05
Well, for asthetic purposes mainly. But I didn't say that there would be only one column in the view :p There will be a between two and four, depending on the tab being viewed.
But QListView can display only a single column... You should use QTreeView if you want more.


But anyway, it seems to be possible for a QAbstractListView, since it inherits from the QAbstractItemModel - and that has a headerData() method.
QAbstractListView supports a single column data only as well.

If you have speed problems, don't use setData to update the model but create a custom method which will insert all items at once. Otherwise all your views will update each time you use setData() or insertRows().

Valheru
5th October 2006, 22:30
Woah. I just subclassed a QAbstractItemView and now I just destroy the old model and set the new model with all the data added in by the constructor of the model. It's damned near instant : it took about 6 seconds to add them to a QStandardItemModel, compared to less than a second!
Only drawback is now that I have to sort my QStringList and then parse it in a for-loop now to sort the newsgroups, which slows things down again a lot, but it still only takes about 15 seconds to parse and display 176.000+ this way, and the GUI stays more or less responsive now :D I added in a model QProgressDialog though to show progress. I normally avoid modals like the plague, but in this case it's warranted I think :)
Thanks for all the tips guys. This forum is a really big help.