Hello,

I am trying to get my data on the clipboard so it can be past it in a text editor and a spreadsheet program. In both situations I would like the paste to be a single click; in contrast with the paste special of Excel that allows you to select a different format from the clipboard.

I have the code below that adds both CSV format and text format on the clipboard.

If I paste it in Excel it looks like the text format is chosen and thus I loose my formatting. In notepad++ I get what I expect.

Using ClipBoardViewer I can see my data on the clipboard in the way I expect it.

Anyone has experience with this situation?

How can I get both text and CSV format on the clipboard so that a text editor chooses the text and a spreadsheet program uses the CSV format?

For me converting the data to HTML is not an option. I say this because I tested html format and it looks that that type get precedence over text while pasting into Excel.

Qt Code:
  1. {
  2. QString selectedCSV = "Fruit,Cars\n" \
  3. "12,13\n" \
  4. "14,15\n";
  5. QString selectedText = "Fruit Cars\n" \
  6. " 12 13\n" \
  7. " 14 15\n";
  8.  
  9. QByteArray copyToClipboardText;
  10. copyToClipboardText.append(selectedText);
  11.  
  12. QByteArray copyToClipboardCSV;
  13. copyToClipboardCSV.append(selectedCSV);
  14.  
  15. QMimeData *mimeData = new QMimeData();
  16.  
  17. // Use all mimetypes that Excel could possibly think of
  18. mimeData->setData ("CSV", copyToClipboardCSV);
  19. mimeData->setData ("Csv", copyToClipboardCSV);
  20. mimeData->setData ("csv", copyToClipboardCSV);
  21. mimeData->setData ("text/CSV",copyToClipboardCSV);
  22. mimeData->setData ("text/Csv",copyToClipboardCSV);
  23. mimeData->setData ("text/csv",copyToClipboardCSV);
  24.  
  25. mimeData->setData ("text/plain", copyToClipboardText);
  26.  
  27. QApplication::clipboard ()->setMimeData(mimeData);
  28. }
To copy to clipboard, switch view to plain text mode