PDA

View Full Version : problem in removing row in QAbstractTableModel



wagmare
28th November 2012, 13:43
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) {
QStringList items;
for (int column = 0; column < columns; ++column)
items.append("");
rowList.insert(position, items);
}

endInsertRows();
return true;
}


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;
}


and in my main window
the slot will call minimum 15 times


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);
}


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);

}

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

amleto
28th November 2012, 13:59
for(int i =0; i < ui.targetTable->model()->rowCount(); i++)
ui.targetTable->model()->removeRow(i);
Think about this for a while, or even check what it does in your debugger. It will not be doing what you intend...

wagmare
29th November 2012, 05:27
for(int i =0; i < ui.targetTable->model()->rowCount(); i++)
ui.targetTable->model()->removeRow(i);
Think about this for a while, or even check what it does in your debugger. It will not be doing what you intend...

thanks for the reply first ..
i know i did something wrong .. in the way i try to remove the row . i m new to this Model/View table things ... first time to say. could u please elaborate it more ..

Did i need QModelIndex to remove the rows ..?

amleto
29th November 2012, 07:06
its not a problem with using Qt. It's a logic problem. Use your debugger! Learning how to use a debugger is not optional!

wagmare
29th November 2012, 09:50
mean like this


for(int i = model->rowCount() -2 ; i >= 0; i -=1)
{
model->removeRow(i,QModelIndex());


}

wysota
29th November 2012, 10:04
That won't delete the last row.

wagmare
29th November 2012, 10:11
That won't delete the last row.

Sorry , sorry ..
for(int i = model->rowCount() -1 ; i >= 0; i -=1)
{
model->removeRow(i,QModelIndex());


}

wysota
29th November 2012, 10:17
Why not simply:


model->removeRows(0, model->rowCount());

?

amleto
29th November 2012, 13:19
and a c++ tidbit:

i -= 1;
is normally written
--i;