PDA

View Full Version : beginMoveRows



StefanLatsch
6th December 2010, 12:57
I have a QSqlQueryModel based Tableview and I want the user sort the resultset in the the tableview manually. So I try to use

beginMoveRows(index, r, r, index, r-1);
endMoveRows();

but it work only partially. The selection in the tableview is moving to the new position, where the row shall be moved. but the content is not moving.

franz
6th December 2010, 13:02
Did you actually remove the content in the model?

StefanLatsch
6th December 2010, 13:12
No I didn't
CODE WHILE INIT THE DIALOG:

model = new AuslagerQueryModel(this);
ui.Liste->setModel(model);
ui.Liste->setSelectionBehavior(QAbstractItemView::SelectRows );

The Testfunction to move a row 1 up

void Auslagerung::OnF6(void)
{
if(model->rowCount() <= 0)
return;
QModelIndex idx = ui.Liste->currentIndex();
model->moveRow(QModelIndex(), idx.row(), -1);

}


void AuslagerQueryModel::moveRow(const QModelIndex &index, const int &r, const int &p)
{
if(r > 0 && p == -1)
{
beginMoveRows(index, r, r, index, r-1);
endMoveRows();
}
}

franz
6th December 2010, 14:21
You only tell the outside world you are moving your data around, while your current implementation doesn't actually do that. You need to actually move your data to the new position in your internal data structure as well.

Also, please use the code tags when posting code.

StefanLatsch
6th December 2010, 14:38
looks like.. but I cant see where I have to move my data. I thought, that beginMove and endmove is doing it and forwarding the changes to the subclasses. may you can give me a hint.

franz
6th December 2010, 15:23
beginMoveRows() and endMoveRows() is just a signaling to the outside world that the model is going to move around data. I am not familiar enough with QSqlQueryModel to give you pointers on how to handle the data. There's a few things you should try (in order) :
Call moveRows() on the QSqlQueryModel()
If you are sorting on some key, do the sorting in your sql query
Make a lower level implementation:

If the move has to be permanent, you'll need to implement your own sql query handling model
If the move is only for the display, implement a proxy model that sorts in your desired order.

StefanLatsch
7th December 2010, 09:49
Thanks,
I extended my sql query model class to keep the data herein. now it works fine...no need to implement another proxy class.