[PySide] Fwd: Working with the clipboard cross-plattafrom

Zahari Dim zaharid at gmail.com
Fri Oct 25 13:54:29 CEST 2013


---------- Forwarded message ----------
From: Zahari Dim <zaharid at gmail.com>
Date: Tue, Oct 15, 2013 at 3:23 PM
Subject: Re: [PySide] Working with the clipboard cross-plattafrom
To: Aaron Richiger <a.richi at bluewin.ch>


Hi Aaron,

Your example works well. However, when I try to paste plain text to the
paste app with my set_clipboard function, it freezes. It does work with
PyQt4, though.

I don't know in which way to manage the event loop so that it allows me to
run the function in an interactive way (ie without entering the qt event
loop forever) and if possible also inside ipython qtconsole (spyder) when
ran with pyside. I have tried something like this (with quite some
tweaking) to no avail:



def start_app():
    app = QtCore.QCoreApplication.instance()
    if app is None:
        print "app"
        app = QtGui.QApplication([])
    return app

def exec_app(app):
    try:
        from IPython.lib.guisupport import start_event_loop_qt4
        start_event_loop_qt4(app)
    except ImportError:
        app.exec_()


def run_in_qt (f, *args, **kwargs):
    def _f(*args, **kwargs):
        app = start_app()
        def _exe():
            f(*args, **kwargs)
            app.quit()
        QtCore.QTimer.singleShot(1, _exe)
        exec_app(app)
    return _f


@run_in_qt
def set_clipboard(content, mime = 'text/plain'):
    #app=QtGui.QApplication.instance() # checks if QApplication already
exists
    #if not app: # create QApplication if it doesnt exist
    #    app = QtGui.QApplication(sys.argv)

    mymime = QtCore.QMimeData()
    mymime.setData(mime, QtCore.QByteArray(content.encode('utf-8')))
    app = start_app()
    clipboard = app.clipboard()
    clipboard.setMimeData(mymime)


The function start_event_loop_qt4(app) basically exec_'s the app only if
none is already running. I can't understand what does pyside do with the
registers in which it stores the clipboard. For example, I can paste after
I close your copy app, but it's not the same here...


On Tue, Oct 15, 2013 at 2:15 AM, Aaron Richiger <a.richi at bluewin.ch> wrote:

>  Ah... From your code, I thougt plaintext to be enough. For different
> mime types, qt may be the best solution:-/
> Did you try my example with USE_PYPERCLIP = False? Because this uses qt
> and works perfectly on my machine and I guess it would do so too when using
> other mime types.
> Aaron
>
> Am 15.10.2013 02:10, schrieb Zahari Dim:
>
> Hi Aaron,
>
> Thank you for your reply! The problem with pyperclip is that unfortunately
> it doesn't support custom mime types.
>
> I would like to be able to something like set_clipboard(lyxcontent,
> mime=«application/x-lyx») to paste content for different applications which
> treat differently their mimes and plain text. I am not aware of any easier
> toolkit for this problem...
> Best regards,
>
> Zahari
> On Oct 15, 2013 2:00 AM, "Aaron Richiger" <a.richi at bluewin.ch> wrote:
>
>>  Hello Zahari!
>>
>> Sorry, just saw, that the USE_PYPERCLIP flag was set to True in the files
>> I sent you. So change it to False first to be consistent with
>> clipboard_copy.py and then change both to True. Sorry for the noise, sent
>> you a previous version...
>> 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 listPySide at qt-project.orghttp://lists.qt-project.org/mailman/listinfo/pyside
>>
>>
>>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/pyside/attachments/20131025/ffa1cf97/attachment.html>


More information about the PySide mailing list