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);
m_pDetailView->setDragEnabled(TRUE);
m_pDetailView->setAcceptDrops(TRUE);
m_pDetailView->setDropIndicatorShown(TRUE);
To copy to clipboard, switch view to plain text mode
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;
}
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;
}
To copy to clipboard, switch view to plain text mode
Please guide me through the correct way. I've tried everything i could.
Thanks in advance.
Bookmarks