[PySide] Working with the clipboard cross-plattafrom

Aaron Richiger a.richi at bluewin.ch
Tue Oct 15 01:57:47 CEST 2013


Hello Zahari!

First my personal meaning when working with Python + Qt:

- Qt is great for classical GUI tasks, so solve graphical things using qt
- BUT: As soon as it comes to surrounding task (networking, etc.), Qt 
offers tools too which may be great helpers in the C++ world, but since 
we are using python, my experience is, that other libraries are much 
more flexible and comfortable to use.

Your question about cross platform use of the clipboard could be one of 
the tasks where it is better to use other python tools: pyperclip. In 
the attachment, you can find 3 files, one containing an app for copying 
to the clipboard, one containing an app for pasting from the clipboard 
and one file containing the code of pyperclip. Start the apps for 
copying and pasting in parallel (I tried it on my Ubuntu by running the 
scripts from two different terminals). Per default, they use Qt to copy 
to the clipboard. By changing the USE_PYPERCLIP variable in both 
scripts, you can achieve the same using pyperclip. On my machine, both 
ways work perfectly fine. But on yours probably only the pyperclip 
approach will work without freeze/crash!?

Hope it helps!
Aaron




Am 15.10.2013 00:45, schrieb Zahari Dim:
> Hi,
>
> I need to programatically fill the clipboard with content that has 
> custom mime types. I could make it work with PyQt4 (with some 
> cabarets). But when I try the same for PySide, the application I try 
> to paste to just freezes, both under windows and linux. How could  
> make this work with PySide:
>
>
> #This works
> from PyQt4 import QtCore, QtGui
> #This doesn't
> #from PySide import QtCore, QtGui
>
> def start_app():
>     app = QtCore.QCoreApplication.instance()
>     if app is None:
>         print "app"
>         app = QtGui.QApplication([])
>     return app
>
>
> #@run_in_qt
> def set_clipboard(content, mime = 'text/plain'):
>
>     mymime = QtCore.QMimeData()
>     mymime.setData(mime, QtCore.QByteArray(content.encode('utf-8')))
>     app = start_app()
>     clipboard = app.clipboard()
>     clipboard.setMimeData(mymime)
>
>
> def get_clipboard():
>     content = QtGui.QApplication.clipboard().mimeData()
>     avaiable_formats = content.formats()
>     return {fmt:content.data(fmt) for fmt in avaiable_formats}
>
>
> #PyQt4 doesn't work without this line _outside_ any of the functions....
> __app = start_app()
>
>
> Any help would be appreciated.
>
> Zahari Dimitrov.
>
>
>
>
>
>
> _______________________________________________
> PySide mailing list
> PySide at qt-project.org
> http://lists.qt-project.org/mailman/listinfo/pyside

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/pyside/attachments/20131015/9bdd1ac5/attachment.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: pyperclip.py
Type: text/x-python
Size: 5604 bytes
Desc: not available
URL: <http://lists.qt-project.org/pipermail/pyside/attachments/20131015/9bdd1ac5/attachment.py>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: clipboard_paste.py
Type: text/x-python
Size: 949 bytes
Desc: not available
URL: <http://lists.qt-project.org/pipermail/pyside/attachments/20131015/9bdd1ac5/attachment-0001.py>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: clipboard_copy.py
Type: text/x-python
Size: 930 bytes
Desc: not available
URL: <http://lists.qt-project.org/pipermail/pyside/attachments/20131015/9bdd1ac5/attachment-0002.py>


More information about the PySide mailing list