PDA

View Full Version : QListView only displaying last item



Easyman
22nd May 2009, 13:32
Hi everyone,

I am totally new to QT(Jambi) and tried to create a simple application with a QListView
that should contain some numbers. Unfortunately only the last number is displayed.
What am I doing wrong?

Here is the code of my two classes:


import java.util.ArrayList;
import java.util.List;
import com.trolltech.qt.gui.QApplication;
import com.trolltech.qt.gui.QMainWindow;
import com.trolltech.qt.gui.QWidget;

public class ListBox extends QMainWindow
{
Ui_MainWindow ui = new Ui_MainWindow();

public static void main(String[] args)
{
QApplication.initialize(args);
ListBox gnah = new ListBox();
gnah.show();
QApplication.exec();
}

public ListBox()
{
ui.setupUi(this);
initialize();
}

public ListBox(QWidget parent)
{
super(parent);
ui.setupUi(this);
initialize();
}

private void initialize()
{
List<Integer> data = new ArrayList<Integer>();
data.add(42);
data.add(666);
data.add(1337);

ListBoxModel model = new ListBoxModel(data);

this.ui.listView.setModel(model);
}
}


import java.util.List;
import com.trolltech.qt.core.QAbstractListModel;
import com.trolltech.qt.core.QModelIndex;
import com.trolltech.qt.core.Qt.ItemDataRole;

public class ListBoxModel extends QAbstractListModel
{
private List<Integer> data;

public ListBoxModel(List<Integer> data)
{
this.data = data;
}

@Override
public Object data(QModelIndex arg0, int arg1)
{
if(arg1 == ItemDataRole.DisplayRole)
{
return data.get(arg0.row());
}
else
{
return null;
}
}

@Override
public int rowCount(QModelIndex arg0)
{
return data.size();
}
}

wysota
24th May 2009, 11:42
I don't know how it looks in Java, but rowCount() and data() are const methods, maybe that's something you forgot about. Are your implementations being called at all?