8

How can one access the internal QGIS clipboard (defined in C++ qgsclipboard) via Python/PyQGIS?

I've tried QgsApplication.clipboard() and QApplication.clipboard, both of which return a PyQt5 QClipboard object rather than a QgsClipboard.

I'd love to make use of features copied to the clipboard in native format (via the copyOf() method of QgsClipboard) but QClipboard seems to have only a text() representation.

Taras
  • 32,823
  • 4
  • 66
  • 137
Houska
  • 7,976
  • 19
  • 48
  • 1
    I think it's a bug, since this method should return a QgsClipboard https://github.com/qgis/QGIS/blob/94e4de59aa846ac7b1e9799fb516bd3f03a00679/src/app/qgisapp.cpp#L12483 maybe it doesn't have a wrapper for python this function?I am thinking aloud – Fran Raga May 15 '19 at 18:01
  • Fran, seems it's worse than that. Browsing the C++ API docs and the Python bindings, the QgsApplication has no mention of the clipboard and QgsClipboard is not listed as a class at all. So it's possible access was not anticipated for plugins or console....still hoping for a better alternative than having to convert the text(), which can be GeoJSON or WKT based on settings. – Houska May 16 '19 at 14:36

2 Answers2

4

In theory you should be able to get the data via the mimeData() associated with the QClipboard

import PyQt5.QtCore
QgsApplication.clipboard().mimeData().retrieveData('application/octet-stream', QVariant.ByteArray)

I get the following error with that running directly from the console. If it's binary (a ByteArray) you're after you might need to experiment to see what MIME type you need to provide.

RuntimeError: no access to protected functions or signals for objects not created from Python

but you might have more luck if running this inside a plugin?


It looks like there's a SIP wrapper involved, one of the functions (retrievedata) is protected visibility so it might not be possible to access this via Python. See this page

PolyGeo
  • 65,136
  • 29
  • 109
  • 338
Steven Kay
  • 20,405
  • 5
  • 31
  • 82
  • Great idea. However, unfortunately QgsApplication.clipboard().mimeData().formats() seems to imply it's only accessible as plain text, which is either GeoJSON or WKT depending on the setting of Settings -> Options -> Data sources -> Copy features as... – Houska May 16 '19 at 14:31
0

As asked by Arthur Yakovlev and answered by azalea and then by styvane, the working solution is (clearly to me and many others) given at https://stackoverflow.com/questions/29259923/pyqt5-cannot-import-name-qapplication.

Arthur Yakovlev who asked the question there, wanted to copy an image and he has also accepted the solution and there are many likes on the question and on the answers.

Azalea's and styvane's answers also point out that the in QGIS the QgsApplication is in PyQt5.QtWidgets. You may find also the https://doc.qt.io/qtforpython-5/PySide2/QtWidgets/QApplication.html documentation useful.

I tried the solution, given by azalea and then by styvane, via a plugin in QGIS 3.32.0 and in QGIS 3.28.4, though I tried to copy only text, and it worked.

Orienteerix
  • 549
  • 1
  • 5
  • 15
  • Thank you for taking the time to answer. Unfortunately, I've read this and the linked stackoverflow question twice, but can't figure out how it related to the question I posed. Can you connect the dots? As far as I can tell, what you've linked to discusses (more generally) how to find (import) QApplication, while in my instance I have QApplication and QgsApplication but have trouble with its QClipboard object. – Houska Jul 17 '23 at 11:27
  • @Houska You are right, the linked stackoverflow question does not answer you your question. I am sorry, of the incorrect answer. – Orienteerix Jul 19 '23 at 06:27