PDA

View Full Version : Drag an drop between differents QTableWidget...



webquinty
14th May 2013, 16:41
Hello,

I have some problems with drag and drop between differents QTableWigdet.
Table A is source information, where I selected and drag the entire row but the last cell.

9035

Now, I would like to drop this information in Table B. (botton QTableWidget).
I have implemented dragEnterEvent, dragMoveEvent, dropEvent and dragLeaveEvent and it works fine.



#include <QtGui>
#include <QDebug>
#include "LeiTableWidget.h"

LeiTableWidget::LeiTableWidget(QWidget *parent)
: QTableWidget(parent)
{
setAcceptDrops(true);
setAutoFillBackground(true);
clear();
}

void LeiTableWidget::dragEnterEvent(QDragEnterEvent *event)
{
// qDebug() << "dragEnterEvent";
event->acceptProposedAction();
}

void LeiTableWidget::dragMoveEvent(QDragMoveEvent *event)
{
// qDebug() << "dragMoveEvent";
event->acceptProposedAction();
}

void LeiTableWidget::dropEvent(QDropEvent *event)
{
qDebug() << "dropEvent";

QTableWidget* table = qobject_cast<QTableWidget*>(event->source());

QPoint old_coordinates = QPoint(-1,-1);
if(table->currentItem() != NULL)
{
// Tengo las coordenadas del elemento que se ha arrastrado desde su origen
//************************************************** ************************
old_coordinates = QPoint( table->currentItem()->row(), table->currentItem()->column() );
}

qDebug() << table->currentItem()->row() << "-" << table->currentItem()->column() ;

qDebug() << "mine data " << event->mimeData()->text();
QTableWidget::dropEvent(event);
event->acceptProposedAction();
}

void LeiTableWidget::dragLeaveEvent(QDragLeaveEvent *event)
{
// qDebug() << "dragLeaveEvent";
event->accept();
}

void LeiTableWidget::clear()
{

}

but now I have some problems:

1.- If a drop in a cell different than column 0, write incorrect data in table.
9036

2.- How can I detect what information or items has drop action?
3.- How can I detect what row is select to drop?

Best regards.

Santosh Reddy
15th May 2013, 15:37
Try implementing dropMimeData instead of dropEvent

See if this works



bool LeiTableWidget::dropMimeData (int row, int column, const QMimeData * data, Qt::DropAction action)
{
return QTableWidget::dropMimeData (row, 0, data, action);
}