Copy to clipboard in plain text format also

The plain text copy is written generically and should work with any
model that is attached to a XTableView
This commit is contained in:
Srivats P 2020-03-18 20:50:29 +05:30
parent 3b31a4fade
commit e6213a90ac
2 changed files with 38 additions and 2 deletions

View File

@ -158,8 +158,8 @@ To emulate a device, click on Configuration and create a device group</string>
<property name="whatsThis">
<string>IP neighbor cache is empty</string>
</property>
<property name="selectionMode">
<enum>QAbstractItemView::SingleSelection</enum>
<property name="selectionBehavior">
<enum>QAbstractItemView::SelectRows</enum>
</property>
</widget>
</item>

View File

@ -83,6 +83,7 @@ public slots:
std::sort(selected.begin(), selected.end());
QMimeData *mimeData = model()->mimeData(selected);
copyPlainText(selected, mimeData);
qApp->clipboard()->setMimeData(mimeData);
qDebug("Copied data in %d format(s) to clipboard",
mimeData->formats().size());
@ -150,6 +151,41 @@ protected:
}
private:
void copyPlainText(const QModelIndexList &indexes, QMimeData *mimeData)
{
if (mimeData->hasText())
return;
bool includeHeaders = (selectionBehavior()
!= QAbstractItemView::SelectItems);
QString text;
if (includeHeaders) {
text.append("\t"); // column header for row number/title
for (int i = 0; i < model()->columnCount(); i++)
text.append(model()->headerData(i, Qt::Horizontal)
.toString()+"\t");;
text.append("\n");
}
int lastRow = -1;
foreach(QModelIndex index, indexes) {
if (index.row() != lastRow) { // row changed
if (lastRow >= 0)
text.append("\n");
if (includeHeaders)
text.append(model()->headerData(index.row(), Qt::Vertical)
.toString()+"\t");
}
else
text.append("\t");
text.append(model()->data(index).toString());
lastRow = index.row();
}
text.append("\n");
mimeData->setText(text);
}
bool _modelAllowsRemove{false};
};