You can do Excel-like copy-paste format of passing table data through clipboard:
Implementation of copy() is approximately is:
int t = model->rowCount();
int b = -1;
int l = model->columnCount();
int r = -1;
QList<QModelIndex> indexes = view->selectionModel()->selection().indexes();
foreach(QModelIndex index, indexes) {
t = qMin(t, index.row());
b = qMax(b, index.row());
l = qMin(l, index.column());
r = qMax(r, index.column());
}
if ( r <0 ) return;
if ( b <0 ) return;
QString data = "<!--StartFragment-->\n";
data += "<table>";
for (int row=t;row<=b;row++) {
data += "<tr>\n";
for (int col=l;col<=r;col++) {
QVariant v = model->data( model->index(row,col) );
if ( v.canConvert(QVariant::Double) ) data += " <td x:num>";
else data += " <td>";
data += v.toString();
data += "</td>\n";
}
data += "</tr>\n";
}
data += "</table>";
data += "<!--EndFragment-->\n";
QMimeData * mimeData = new QMimeData;
mimeData->setHtml( data );
QApplication::clipboard()->setMimeData(mimeData);
For paste() you need to implement just simple parsing of such html table.
Then you can do copy-paste to/from Excel too or other table-like apps.
Another standard way (which is also supported by Excel and other apps working with table) is plain text QClipboard data tab-delimited for cell separators. Code for forming tab-delimited text is pretty similar, but parsing for paste() is even simplier.