I think I found a solution to the problem.
The QTableWidget uses a special MIME type which can't be used by other controls/applications but there is a way to add additional MIME types by overwriting the following methods:
bool DragAndDropTableWidget
::dropMimeData(int row,
int column,
const QMimeData *data, Qt
::DropAction action
) {
if(data->hasText())
{
if(item != 0)
item->setText(data->text());
return false;
}
else
{
return QTableWidget::dropMimeData(row, column, data, action
);
}
}
{
return QTableWidget::mimeTypes() << QStringLiteral
("text/plain");
}
QMimeData *DragAndDropTableWidget
::mimeData(const QList<QTableWidgetItem
*> items
) const {
if(items.count() > 0)
{
{
if(!item->text().isEmpty())
{
text.append(item->text());
if(item != lastItem)
{
text.append(QStringLiteral("\n"));
}
}
}
}
data->setText(text);
return data;
}
bool DragAndDropTableWidget::dropMimeData(int row, int column, const QMimeData *data, Qt::DropAction action)
{
if(data->hasText())
{
QTableWidgetItem *item = this->item(row, column);
if(item != 0)
item->setText(data->text());
return false;
}
else
{
return QTableWidget::dropMimeData(row, column, data, action);
}
}
QStringList DragAndDropTableWidget::mimeTypes() const
{
return QTableWidget::mimeTypes() << QStringLiteral("text/plain");
}
QMimeData *DragAndDropTableWidget::mimeData(const QList<QTableWidgetItem *> items) const
{
QString text;
if(items.count() > 0)
{
QTableWidgetItem *lastItem = items.last();
foreach(QTableWidgetItem *item, items)
{
if(!item->text().isEmpty())
{
text.append(item->text());
if(item != lastItem)
{
text.append(QStringLiteral("\n"));
}
}
}
}
QMimeData *data = QTableWidget::mimeData(items);
data->setText(text);
return data;
}
To copy to clipboard, switch view to plain text mode
I'm adding the MIME type "text/plain" which is used by most applications and it seems to work (even if there are multiple cells selected).
There's no use in overwriting dropEvent, dragMoveEvent and dragEnterEvent.
If someone has suggestions to improve the code, let me know because I'm not sure how to deal with that methods due I found no examples about them and the documentation doesn't give a lot of information, too.
Bookmarks