PDA

View Full Version : QListView remains empty



postb99
13th February 2014, 16:00
Hello, I use Qt 4.8 for a project and had troubles so set up a sample project because as a newbie I have troubles finding out what's wrong.

I use a QListView to show items information. In my real project, there will be item properties such as ID, name etc. In sample project I made a simple list of strings.

So why is the list empty when I run the sample app?

Thank you very much,

Here is the code:

https://gist.github.com/postb99/548b32546eece2e7aed6

anda_skoa
13th February 2014, 19:34
The problem is that you derive from QStandardItemModel.

When its index() method is called, it will always return a null index because it is empty.

Derive from QAbstractTableModel instead, or QAbstractListModel if you want a list.

In a full implementation your addItem() method would have to signal that rows are being added, see beginInsertRows(), endInsertRows().

In your simple example it should still be OK, since you add both items before you set the model on the view.

Cheers.
_