void ExtendedTableWidget::copy() { QModelIndexList indices = selectionModel()->selectedIndexes(); // Abort if there's nothing to copy if (indices.isEmpty()) return; qSort(indices); SqliteTableModel* m = qobject_cast<SqliteTableModel*>(model()); // If a single cell is selected, copy it to clipboard if (indices.size() == 1) { QImage img; QVariant data = m->data(indices.first(), Qt::EditRole); if (img.loadFromData(data.toByteArray())) { // If it's an image qApp->clipboard()->setImage(img); return; } else { qApp->clipboard()->setText(data.toString()); return; } } m_buffer.clear(); // If any of the cells contain binary data - we use inner buffer bool containsBinary = false; foreach (const QModelIndex& index, indices) if (m->isBinary(index)) { containsBinary = true; break; } if (containsBinary) { qApp->clipboard()->clear(); // Copy selected data into inner buffer int columns = indices.last().column() - indices.first().column() + 1; while (!indices.isEmpty()) { QByteArrayList lst; for (int i = 0; i < columns; ++i) { lst << indices.first().data(Qt::EditRole).toByteArray(); indices.pop_front(); } m_buffer.push_back(lst); } return; } QModelIndex first = indices.first(); QString result; int currentRow = 0; foreach(const QModelIndex& index, indices) { if (first == index) { /* first index */ } else if (index.row() != currentRow) result.append("\r\n"); else result.append("\t"); currentRow = index.row(); QVariant data = index.data(Qt::EditRole); // non-NULL data is enquoted, whilst NULL isn't if (!data.isNull()) { QString text = data.toString(); text.replace("\"", "\"\""); result.append(QString("\"%1\"").arg(text)); } } qApp->clipboard()->setText(result); }