tags:

views:

1374

answers:

1

Hello,

I am slowly getting used to using the Qt4 GUI framework. In a project I am working on, I need to be able to add/edit/remove Team objects in a list. Coming from a C#.NET perspective, I would do something like

List<Team> teams = new List<Team>();
teamsListBox.DataSource = teams;
teamsListBox.DisplayMember = "Name";

Then use buttons on the form to do the adding/removing/editing.

But, from what I can tell, there is no easy way to do this in Qt. I have looked over the documentation for QListView, QListWidget, QStandardItemModel, etc. but I cannot figure out how to get the equivalent Qt code for the C#.

My objective is to show the Teams in a list box of some kind, then be able to add/remove/edit the Teams underneath it at runtime.

How would you do this?

+2  A: 

You should have a look at QAbstractItemModel and QStandardItemModel or create a customized TeamItemModel class for your teams that inherits from QAbstractItemModel. Those customized class will manage how the items are displayed in the Widget like QListView.

A simple for QString item example with QStringList:

QStringList list;
list << "item1" << "item2" << "item3" << "item4" << "item5";
ui->listView->setModel(new QStringListModel(list));

Then adding/removing/updating a Team should be easier than what you have tried.

Hope that helps.

Patrice Bernassola
Is there an easier way that avoids subclassing? Like creating a model from a `QList<Team>`?
Austin Hyde
There are DirModel, ProxyModel and StandardItemModel but no list model. I think You have to subclass QAbstractItemModel for your need.
Patrice Bernassola
Patrice, can you give a sample code with QStandardItemModel containing more than 1 column and few rows for QListView ?
Samir
Have you seen the sample in the Trolltech doc? http://doc.trolltech.com/4.1/qstandarditemmodel.html#details
Patrice Bernassola