PDA

View Full Version : Drag & drop for QTreeView



yogeshm02
29th January 2006, 15:44
Hi

I'm having dificulty using drag & drop for QTreeView. Actually i'm trying to move an item by drag n drop. I think i've setup everything as documented in qt4 docs. See the code below, may be you could find mistakes.

So far i've noticed following misbehaviour: -

When an item is moved, a new copy of the item is created.
When i start dragging an item from say first column and drap at say second column, a new copy of the item is created with text of column1 in column2, column 2 in column3, so on....


Set up QTreeView like this: -


m_pDetailView->setDragEnabled(TRUE);
m_pDetailView->setAcceptDrops(TRUE);
m_pDetailView->setDropIndicatorShown(TRUE);


Have following in QAbstractItemMode derived class


Qt::ItemFlags NowPlayingModel::flags(const QModelIndex& index)const{
if(!index.isValid())
return Qt::ItemIsEnabled | Qt::ItemIsDropEnabled;
Qt::ItemFlags flags = Qt::ItemIsSelectable | Qt::ItemIsEnabled | Qt::ItemIsDragEnabled;
if(mColumns.at(index.column()).isEditable)
flags |= Qt::ItemIsEditable;
return flags;
}

Qt::DropActions NowPlayingModel::supportedDropActions() const{
return Qt::CopyAction | Qt::MoveAction;
}
//This function is never called
bool NowPlayingModel::removeRows(int row, int count, const QModelIndex &parent){
qDebug("Remove");
if(parent.isValid())
return FALSE;
beginRemoveRows(parent, row, row + count - 1);
for(int i = 0; i < count; ++i){
delete m_pAllData->takeAt(row);
}
endRemoveRows();
return TRUE;
}


Please guide me through the correct way. I've tried everything i could.

Thanks in advance.

wysota
29th January 2006, 18:14
First of all make sure that you have "SelectRows" chosen in "Select Behavior" of the tree view. This will hopefully solve the problem with first and second column.

As for the rest:

Qt::DropActions NowPlayingModel::supportedDropActions() const{
return Qt::CopyAction | Qt::MoveAction;
}

This implies that you accept a copy action. If you don't want the item to be copied, make sure you only accept moving items.

You might also want to reimplement the tree view's dropEvent to check wheather the item was dropped on the same widget it was picked from.

yogeshm02
30th January 2006, 15:32
First of all make sure that you have "SelectRows" chosen in "Select Behavior" of the tree view. This will hopefully solve the problem with first and second column.

Hey, it works ;)


As for the rest:

Qt::DropActions NowPlayingModel::supportedDropActions() const{
return Qt::CopyAction | Qt::MoveAction;
}

This implies that you accept a copy action. If you don't want the item to be copied, make sure you only accept moving items.

If i return Qt::MoveAction, i am unable to drop the item anywhere in the tree view.

Perhaps there is something wrong in the manupulation of the model data. I'll check and post back with result.

With regards