Hi, hoping some people could help me with some drag and drop of mime data
I have a treeview and a standardItemModel I have subclassed.
I'd like to drag and drop within the view to sort the items.
And a line to indicate where would be perfect.
I've looked at this thread and I've picked up lots of ideas.
http://www.qtcentre.org/forum/f-qt-p...icon-2304.html
However I cant make it work..
spent a few days trying
If I implement only these :
{
dropSite = event->answerRect ( );
model_->emit_layoutChanged();
}
void MyModel::emit_layoutChanged ( )
{
emit layoutChanged ( );
}
{
int x, y, w, h;
dropSite.getRect ( &x, &y, &w, &h );
painter.drawLine ( 0, y, width(), y );
event->accept();
}
void MyTreeView::dragMoveEvent (QDragMoveEvent *event)
{
dropSite = event->answerRect ( );
model_->emit_layoutChanged();
}
void MyModel::emit_layoutChanged ( )
{
emit layoutChanged ( );
}
void MyTreeView::paintEvent ( QPaintEvent* event )
{
QTreeView::paintEvent (event);
QPainter painter ( viewport() );
int x, y, w, h;
dropSite.getRect ( &x, &y, &w, &h );
painter.drawLine ( 0, y, width(), y );
event->accept();
}
To copy to clipboard, switch view to plain text mode
I get a line drawn although it doesnt stick to between items and draws behind at all pixel positions. However Its not a mime data drag and drop and so its a floating icon type which I want to avoid.
Now, if I implement :
{
if (event->button() == Qt::LeftButton) {
dragStartPosition = event->pos();
}
repaint();
}
{
if (!(event->buttons() & Qt::LeftButton))
return;
if ((event->pos() - dragStartPosition).manhattanLength()
return;
// Sample text type mime data - I will use mybe row index to pass data instead
mimeData->setData("application/vnd.text.list", encodedData);
drag->setMimeData(mimeData);
dropAction = drag->start ( Qt::CopyAction | Qt::MoveAction );
repaint();
}
void MyTreeView::mousePressEvent(QMouseEvent *event)
{
QTreeView::mousePressEvent ( event );
if (event->button() == Qt::LeftButton) {
dragStartPosition = event->pos();
}
repaint();
}
void MyTreeView::mouseMoveEvent(QMouseEvent *event)
{
if (!(event->buttons() & Qt::LeftButton))
return;
if ((event->pos() - dragStartPosition).manhattanLength()
< QApplication::startDragDistance())
return;
QByteArray encodedData;
mimeData = new QMimeData;
// Sample text type mime data - I will use mybe row index to pass data instead
mimeData->setData("application/vnd.text.list", encodedData);
drag = new QDrag(this);
drag->setMimeData(mimeData);
dropAction = drag->start ( Qt::CopyAction | Qt::MoveAction );
repaint();
}
To copy to clipboard, switch view to plain text mode
I get a mime type drag and drop which I want but I cannot drop it on the Treeview anywhere. I get the no entry sign 
I have these turned on:
treeView->setDragEnabled(true);
treeView->setDropIndicatorShown(true);
treeView->setAcceptDrops(true);
I guess I need to implement something in here :
bool MyModel
::dropMimeData ( const QMimeData * data, Qt
::DropAction action,
int row,
int column,
const QModelIndex & parent
) {
qDebug("dropMimeData");
if (action == Qt::CopyAction) {
return true;
}
else {
return false;
}
}
bool MyModel::dropMimeData ( const QMimeData * data, Qt::DropAction action, int row, int column, const QModelIndex & parent )
{
qDebug("dropMimeData");
if (action == Qt::CopyAction) {
return true;
}
else {
return false;
}
}
To copy to clipboard, switch view to plain text mode
but this never gets called when I drop.
What am I missing and any tips ? Mybe someone else find this useful.
Thanks,
Bookmarks