Good am!
I have just startet to deal with DnD and I got it working already.
But there is one problem left: The DropIndicator is empty! Means, there is the '+'-sign and a small empty square only while copying an item.
I guess, it's not not a big thing but I was not able to find it out by myself.
Here is some code:
tablewidget.h
{
Q_OBJECT
public:
TableWidget
( QWidget *parent
= 0 );
protected:
private:
TableWidget *tableWidget;
};
class TableWidget : public QTableWidget
{
Q_OBJECT
public:
TableWidget( QWidget *parent = 0 );
protected:
void dropEvent( QDropEvent *event );
void dragEnterEvent( QDragEnterEvent *event );
void dragMoveEvent( QDragMoveEvent* event );
private:
TableWidget *tableWidget;
};
To copy to clipboard, switch view to plain text mode
tablewidget.cpp
#include "tablewidget.h"
TableWidget
::TableWidget( QWidget *parent
){
setAcceptDrops( true );
}
{
itemAt( event->pos() )->setText( event->mimeData()->text() );
event->acceptProposedAction();
}
{
event->acceptProposedAction();
}
{
event->accept();
}
#include "tablewidget.h"
TableWidget::TableWidget( QWidget *parent )
: QTableWidget( parent )
{
setAcceptDrops( true );
}
void TableWidget::dropEvent( QDropEvent *event )
{
itemAt( event->pos() )->setText( event->mimeData()->text() );
event->acceptProposedAction();
}
void TableWidget::dragEnterEvent( QDragEnterEvent *event )
{
event->acceptProposedAction();
}
void TableWidget::dragMoveEvent( QDragMoveEvent* event )
{
event->accept();
}
To copy to clipboard, switch view to plain text mode
treewidget.h
{
Q_OBJECT
public:
protected:
private:
TreeWidget *treeWidget;
};
class TreeWidget : public QTreeWidget
{
Q_OBJECT
public:
TreeWidget( QWidget *parent = 0 );
protected:
void mouseMoveEvent( QMouseEvent *event );
private:
TreeWidget *treeWidget;
};
To copy to clipboard, switch view to plain text mode
treewidget.cpp
TreeWidget
::TreeWidget( QWidget *parent
){
setDragEnabled( true );
}
{
if ( ! ( event->buttons() & Qt::LeftButton ) )
return;
if ( currentItem() == NULL )
return;
mimeData->setText( currentItem()->text( 0 ) );
drag->setMimeData( mimeData );
drag->start( Qt::CopyAction | Qt::MoveAction );
}
TreeWidget::TreeWidget( QWidget *parent )
: QTreeWidget( parent )
{
setDragEnabled( true );
}
void TreeWidget::mouseMoveEvent( QMouseEvent *event )
{
if ( ! ( event->buttons() & Qt::LeftButton ) )
return;
if ( currentItem() == NULL )
return;
QDrag *drag = new QDrag( this );
QMimeData *mimeData = new QMimeData;
mimeData->setText( currentItem()->text( 0 ) );
drag->setMimeData( mimeData );
drag->start( Qt::CopyAction | Qt::MoveAction );
}
To copy to clipboard, switch view to plain text mode
Any idea what's missing?
Bookmarks