DropMimeData is not getting called for extended QTableWidget
-
I have extended QTableWidget to create a custom tableWidget. I want to override the dropMimeData method. But dropMimeData() is not getting called.
Here is my code.
mytablewidget1.h
@
#include <QtGui>class myTableWidget1 : public QTableWidget
{
Q_OBJECT
public:myTableWidget1(QWidget *parent = 0); virtual bool dropMimeData(int row, int column, const QMimeData *data, Qt::DropAction action); QStringList mimeTypes() const; Qt::DropActions supportedDropActions () const;
};@
mytablewidget.cpp
@
#include "mytablewidget1.h"
#include <iostream>myTableWidget1::myTableWidget1(QWidget *parent) :
QTableWidget(3,3,parent)
{
this->dragEnabled();
setSelectionMode(QAbstractItemView::SingleSelection);
setDragDropMode(QAbstractItemView::InternalMove);QTableWidgetItem *item=new QTableWidgetItem(); item->setText("1,1"); setItem(1,1,item); item=new QTableWidgetItem(); item->setText("2,2"); setItem(2,2,item);
}
bool myTableWidget1::dropMimeData(int row, int column, const QMimeData *data, Qt::DropAction action)
{
std::cout<<"in dropMineData......."<<std::endl;return true;
}
QStringList myTableWidget1::mimeTypes () const
{QStringList qstrList; qstrList.append("text/uri-list"); return qstrList;
}
Qt::DropActions myTableWidget1::supportedDropActions () const
{
return Qt::CopyAction | Qt::MoveAction;
}@
main.cpp
@
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
myTableWidget1 *dialog = new myTableWidget1(5,5);dialog->show(); return app.exec();
}@
[Edit: please use @ code tags for code, Eddy]
-
You did not set the drag/drop on the viewport. From the "docs:":http://doc.qt.nokia.com/4.7/model-view-programming.html#using-drag-and-drop-with-item-views
@
QListWidget *listWidget = new QListWidget(this);
listWidget->setSelectionMode(QAbstractItemView::SingleSelection);
listWidget->setDragEnabled(true);
listWidget->viewport()->setAcceptDrops(true);
listWidget->setDropIndicatorShown(true);
@The method you use (dragEnabled) does not enable it, it is a getter.
-
Setting DragDropMode to QAbstractItemView::DragDrop worked for me. Earlier i was using setDragDropMode(QAbstractItemView::InternalMove).