Using QListView or similar in Qt4

I'm slowly getting used to using the Qt4 GUI. In the project I'm working on, I need to be able to add / edit / delete Team objects in the list. Based on the perspective of C # .NET, I would do something like

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

Then use the buttons on the form to add / remove / edit.

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

My goal is to show Team in some list, and then add / remove / edit Team under it at runtime.

How do you do this?

+4
source share
1 answer

You should look at QAbstractItemModel and QStandardItemModel or create an individual TeamItemModel class for your teams, which inherits from QAbstractItemModel. This custom class will control how elements are displayed in a widget such as QListView.

A simple QString example with a 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 you tried.

Hope this helps.

+3
source

All Articles