hi friends,
Im facing a problem of updating my QAbstractTableModel in QTableView .. when i insert few rows and then i try to remove the rows ...
this is my insertRow function in TableModel
bool TableModel
::insertRows(int position,
int rows,
const QModelIndex &parent
) {
int columns = columnCount();
beginInsertRows(parent, position, position + rows - 1);
for (int row = 0; row < rows; ++row) {
for (int column = 0; column < columns; ++column)
items.append("");
rowList.insert(position, items);
}
endInsertRows();
return true;
}
bool TableModel::insertRows(int position, int rows, const QModelIndex &parent)
{
int columns = columnCount();
beginInsertRows(parent, position, position + rows - 1);
for (int row = 0; row < rows; ++row) {
QStringList items;
for (int column = 0; column < columns; ++column)
items.append("");
rowList.insert(position, items);
}
endInsertRows();
return true;
}
To copy to clipboard, switch view to plain text mode
and my removeRows
bool TableModel
::removeRows(int position,
int rows,
const QModelIndex &parent
) {
beginRemoveRows(parent, position, position + rows - 1);
for (int row = 0; row < rows; ++row) {
rowList.removeAt(position);
}
endRemoveRows();
return true;
}
bool TableModel::removeRows(int position, int rows, const QModelIndex &parent)
{
beginRemoveRows(parent, position, position + rows - 1);
for (int row = 0; row < rows; ++row) {
rowList.removeAt(position);
}
endRemoveRows();
return true;
}
To copy to clipboard, switch view to plain text mode
and in my main window
the slot will call minimum 15 times
void
MainWindow::receivedData(const dataStruct &data)
{
model->setData(index, data.No, Qt::EditRole);
model->setData(index, data.x, Qt::EditRole);
model->setData(index, data.y, Qt::EditRole);
model->setData(index, data.z, Qt::EditRole);
}
void
MainWindow::receivedData(const dataStruct &data)
{
model-> insertRows(0, 1, QModelIndex());
QModelIndex index = model->index(0, 0, QModelIndex());
model->setData(index, data.No, Qt::EditRole);
index = model->index(0, 1, QModelIndex());
model->setData(index, data.x, Qt::EditRole);
index = model->index(0, 2, QModelIndex());
model->setData(index, data.y, Qt::EditRole);
index = model->index(0, 3, QModelIndex());
model->setData(index, data.z, Qt::EditRole);
}
To copy to clipboard, switch view to plain text mode
and one slot called one time after it called 15 times the receiveData
void
MainWindow::clearTableSlot()
{
qDebug()<<"Row Counting:"<<ui.targetTable->model()->rowCount();
for(int i =0; i < ui.targetTable->model()->rowCount(); i++)
ui.targetTable->model()->removeRow(i);
}
void
MainWindow::clearTableSlot()
{
qDebug()<<"Row Counting:"<<ui.targetTable->model()->rowCount();
for(int i =0; i < ui.targetTable->model()->rowCount(); i++)
ui.targetTable->model()->removeRow(i);
}
To copy to clipboard, switch view to plain text mode
and again i will try to add rows so i can update it ...
but clearTableSlot() is not not removing the rows ...
its not removing the rows so that
the rows are appending one after another ...
please help and thanks in advance
Bookmarks