Using a QListView or similar effectively in Qt4
I am slowly getting used t开发者_StackOverflow中文版o 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 Team
s in a list box of some kind, then be able to add/remove/edit the Team
s underneath it at runtime.
How would you do this?
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.
精彩评论