Just starting out with Qt Jambi 4.4 (2 days experience) but would consider myself an experienced C# programmer.
Trying to create an implementation of QAbstractListModel and display it in a QListView. After a search it appears I'm having similar problems to zlosynus and azdruid.
I've found that the model will display quite happily when used with setModel() on a QTableView but doing the same thing with a QListView.
Below is the implementation of the model, if anyone has any idea as to what the difference is between QListView and QTableView or if they can see any glaring problems with my code then that'd be great. I want to love Qt Jambi but can't quite yet while I see stuff like this!
{
private List<Foo> _data = new Vector<Foo>();
public FooListModel(List<Foo> data)
{
if (data == null)
{
return;
}
for (Foo foo : data)
{
_data.add(foo);
}
reset();
}
{
if (index.row() < 0 || index.row() >= _data.size())
{
}
Foo foo = _data.get(index.row());
if (foo == null)
{
}
return foo;
}
{
return _data.size();
}
}
public class FooListModel extends QAbstractListModel
{
private List<Foo> _data = new Vector<Foo>();
public FooListModel(List<Foo> data)
{
if (data == null)
{
return;
}
for (Foo foo : data)
{
_data.add(foo);
}
reset();
}
public Object data(QModelIndex index, int role)
{
if (index.row() < 0 || index.row() >= _data.size())
{
return new QVariant();
}
Foo foo = _data.get(index.row());
if (foo == null)
{
return new QVariant();
}
return foo;
}
public int rowCount(QModelIndex parent)
{
return _data.size();
}
}
To copy to clipboard, switch view to plain text mode
An example of how I use the code:
Foo foo = new Foo();
foo.setName("Foo!");
List<Foo> data = new Vector<Foo>();
data.add(foo);
FooListModel fooListModel = new FooListModel(data);
fooListView.setModel(fooListModel);
fooTableView.setModel(fooListModel);
Foo foo = new Foo();
foo.setName("Foo!");
List<Foo> data = new Vector<Foo>();
data.add(foo);
FooListModel fooListModel = new FooListModel(data);
fooListView.setModel(fooListModel);
fooTableView.setModel(fooListModel);
To copy to clipboard, switch view to plain text mode
Thanks for your time...
Bookmarks