PDA

View Full Version : QList<Object>into QListView or QTree



tirolel
18th July 2016, 13:16
Hello everyone.

Since I'm a newbie to QT, I would like to hear if someone can guide or help me to explain the issue I have.
What I need is
I have a class of user, which I creating an object with such as:
myuser = new user(id, name, firstname, lastname);

and I'm appending it into a

QList<users> *userList;
userList.append(myuser);


The question is

I'm creating few dummy users and I want to show them in QListView.

Since I'm new to the QT, I read that I need to create a model to show the objects(users) in to the view.
I couldn't find anywhere some good examples that I can follow.
So what I have to do to show the users in QListView or QTable/Tree view?

anda_skoa
18th July 2016, 13:32
You might have an easier start with a QListWidget, QTableWidget or QTreeWidget.
These allow you to create "items" objects for each entry instead of using a model.
Model's tend to be a bit more complicated.

Cheers,
_

tirolel
18th July 2016, 14:51
thx for replying back.

I have done the list widget and there no problem with that.
Its working good but I want to use the QListView aswell (learning porpuse).

anda_skoa
18th July 2016, 17:14
I see.

Then your next step is to create a subclass of QAbstractListModel and somehow given that object access to your QList.
You will have to implement QAbstractItemModel::rowCount() and QAbstractItemModel::data().

In rowCount() you can ignore the function's argument since you are only implementing a list (argument only needed for trees).

In data() you only need the row information from the index argument.
You can also just return a default constructed QVariant for all roles that are not Qt::DisplayRole

Cheers,
_