Thomas Coopman wrote:
Hi,
I'm trying to write a custom QAbstractTableModel but I have some
problems with inserting data into the model.
My model will have a fixed number of columns and data items can be
added one at a time.
All the items that are present before I start the model get added
correctly. New items that get added in the current row are also shown
correctly but at the moment I have to insert a new row, no more new
items are shown.
I've tried before with both beginInsertColumns and beginInsertRows but
that didn't work.
Hi Thomas,
It seems you did not specify the proper indices to beginInsertRows().
The indices are counted from 0, so to insert one new row you need to
specify:
beginInsertRows(null, rowCount-1, rowCount-1);
In your code you are adding new entries beyond the end of the table
which is not possible, hence it does nothing.
I've attached a working example. It adds a new row every two seconds so
you can see how to use beginInsertRows().
best regards,
Gunnar
import com.trolltech.qt.gui.*;
import com.trolltech.qt.core.*;
public class ItemModel extends QAbstractTableModel {
public ItemModel() {
timer = new QTimer();
timer.timeout.connect(this, "addData()");
timer.start(2000);
}
public int rowCount(QModelIndex parent) { return rows; }
public int columnCount(QModelIndex parent) { return 4; }
public Object data(QModelIndex index, int role) {
if (role == Qt.ItemDataRole.DisplayRole) {
return "col=" + (index.column() + 1) + ", row=" + (index.row() +
1);
}
return null;
}
public void addData() {
System.out.println("Adding data...\n");
beginInsertRows(null, rows - 1, rows - 1);
++rows;
endInsertRows();
}
private int rows = 4;
private QTimer timer;
public static void main(String args[]) {
QApplication.initialize(args);
QTableView view = new QTableView();
ItemModel model = new ItemModel();
view.setModel(model);
view.show();
QApplication.exec();
}
}_______________________________________________
Qt-jambi-interest mailing list
[email protected]
http://lists.trolltech.com/mailman/listinfo/qt-jambi-interest