PDA

View Full Version : Drag and drop items in view to sort order



Big Duck
25th May 2006, 17:48
Hi,

I'd like to sort items in a tree view by dragging and dropping.
My data is a flat list of rows and columns with no heirarchy. I chose not to use TableView as the row headers cant be hidden, is this right ?

I've got as far as subclassing standardItemView and re implementing :
supportedDropActions() const
removeRows(int row, int count, const QModelIndex &parent)

My trouble is what to put in removeRows so that a MoveAction is done on the model. At the moment I get copying of data in the view.

Another problem is that when dragging the treeView item, I get a row with a + as its a treeView Im using, any ideas where I'm going wrong.

Below is what I've got so far




MyModel::MyModel(QObject *parent)
: QStandardItemModel(parent)
{
}

Qt::DropActions MyModel::supportedDropActions() const
{
return Qt::MoveAction;
}

bool MyModel::removeRows(int row, int count, const QModelIndex &parent)
{
if (parent.isValid())
return false;
beginRemoveRows(parent, 0, 0);
// What am I doing here?
endRemoveRows();
return true;
}

wysota
25th May 2006, 19:43
Hi,

I'd like to sort items in a tree view by dragging and dropping.
My data is a flat list of rows and columns with no heirarchy. I chose not to use TableView as the row headers cant be hidden, is this right ?

tableView->horizontalHeader()->hide();


I've got as far as subclassing standardItemView and re implementing :
supportedDropActions() const
removeRows(int row, int count, const QModelIndex &parent)
You don't have to reimplement removeRows(). You have to reimplement removeRow(). the former will call removeRow() for each row which is to be removed, so you don't have to subclass it. removeRow() should... remove a row pointed by the index from the model (meaning its internal representation). For example, if you hold your items in a QList, you should remove an item associated with the index from that list.


My trouble is what to put in removeRows so that a MoveAction is done on the model. At the moment I get copying of data in the view.
Look above.


Another problem is that when dragging the treeView item, I get a row with a + as its a treeView Im using, any ideas where I'm going wrong.
That's because the item is copied as a child of the item you drop it on. You should reimplement dropMimeData() to correct that.