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
[email protected]
http://lists.qt-project.org/mailman/listinfo/pyside
# Pyperclip v1.3
# A cross-platform clipboard module for Python. (only handles plain text for now)
# By Al Sweigart [email protected]
# Usage:
# import pyperclip
# pyperclip.copy('The text to be copied to the clipboard.')
# spam = pyperclip.paste()
# On Mac, this module makes use of the pbcopy and pbpaste commands, which should come with the os.
# On Linux, this module makes use of the xclip command, which should come with the os. Otherwise run "sudo apt-get install xclip"
# Copyright (c) 2010, Albert Sweigart
# All rights reserved.
#
# BSD-style license:
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
# * Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
# * Neither the name of the pyperclip nor the
# names of its contributors may be used to endorse or promote products
# derived from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY Albert Sweigart "AS IS" AND ANY
# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL Albert Sweigart BE LIABLE FOR ANY
# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
# Change Log:
# 1.2 Use the platform module to help determine OS.
# 1.3 Changed ctypes.windll.user32.OpenClipboard(None) to ctypes.windll.user32.OpenClipboard(0), after some people ran into some TypeError
import platform, os
def winGetClipboard():
ctypes.windll.user32.OpenClipboard(0)
pcontents = ctypes.windll.user32.GetClipboardData(1) # 1 is CF_TEXT
data = ctypes.c_char_p(pcontents).value
#ctypes.windll.kernel32.GlobalUnlock(pcontents)
ctypes.windll.user32.CloseClipboard()
return data
def winSetClipboard(text):
GMEM_DDESHARE = 0x2000
ctypes.windll.user32.OpenClipboard(0)
ctypes.windll.user32.EmptyClipboard()
try:
# works on Python 2 (bytes() only takes one argument)
hCd = ctypes.windll.kernel32.GlobalAlloc(GMEM_DDESHARE, len(bytes(text))+1)
except TypeError:
# works on Python 3 (bytes() requires an encoding)
hCd = ctypes.windll.kernel32.GlobalAlloc(GMEM_DDESHARE, len(bytes(text, 'ascii'))+1)
pchData = ctypes.windll.kernel32.GlobalLock(hCd)
try:
# works on Python 2 (bytes() only takes one argument)
ctypes.cdll.msvcrt.strcpy(ctypes.c_char_p(pchData), bytes(text))
except TypeError:
# works on Python 3 (bytes() requires an encoding)
ctypes.cdll.msvcrt.strcpy(ctypes.c_char_p(pchData), bytes(text, 'ascii'))
ctypes.windll.kernel32.GlobalUnlock(hCd)
ctypes.windll.user32.SetClipboardData(1,hCd)
ctypes.windll.user32.CloseClipboard()
def macSetClipboard(text):
outf = os.popen('pbcopy', 'w')
outf.write(text)
outf.close()
def macGetClipboard():
outf = os.popen('pbpaste', 'r')
content = outf.read()
outf.close()
return content
def gtkGetClipboard():
return gtk.Clipboard().wait_for_text()
def gtkSetClipboard(text):
cb = gtk.Clipboard()
cb.set_text(text)
cb.store()
def qtGetClipboard():
return str(cb.text())
def qtSetClipboard(text):
cb.setText(text)
def xclipSetClipboard(text):
outf = os.popen('xclip -selection c', 'w')
outf.write(text)
outf.close()
def xclipGetClipboard():
outf = os.popen('xclip -selection c -o', 'r')
content = outf.read()
outf.close()
return content
def xselSetClipboard(text):
outf = os.popen('xsel -i', 'w')
outf.write(text)
outf.close()
def xselGetClipboard():
outf = os.popen('xsel -o', 'r')
content = outf.read()
outf.close()
return content
if os.name == 'nt' or platform.system() == 'Windows':
import ctypes
getcb = winGetClipboard
setcb = winSetClipboard
elif os.name == 'mac' or platform.system() == 'Darwin':
getcb = macGetClipboard
setcb = macSetClipboard
elif os.name == 'posix' or platform.system() == 'Linux':
xclipExists = os.system('which xclip') == 0
if xclipExists:
getcb = xclipGetClipboard
setcb = xclipSetClipboard
else:
xselExists = os.system('which xsel') == 0
if xselExists:
getcb = xselGetClipboard
setcb = xselSetClipboard
try:
import gtk
getcb = gtkGetClipboard
setcb = gtkSetClipboard
except:
try:
import PyQt4.QtCore
import PyQt4.QtGui
app = QApplication([])
cb = PyQt4.QtGui.QApplication.clipboard()
getcb = qtGetClipboard
setcb = qtSetClipboard
except:
raise Exception('Pyperclip requires the gtk or PyQt4 module installed, or the xclip command.')
copy = setcb
paste = getcbfrom PySide.QtGui import *
from PySide.QtCore import *
import pyperclip
USE_PYPERCLIP = True
class Widget(QWidget):
def __init__(self, parent=None):
super(Widget, self).__init__(parent)
self.setupUi()
def setupUi(self):
self.setLayout(QVBoxLayout())
self.lbOutput = QLabel(self)
self.pbPasteFromClipboard = QPushButton("Copy to clipboard", self)
self.pbPasteFromClipboard.clicked.connect(self.onPasteFromClipboard)
self.layout().addWidget(self.lbOutput)
self.layout().addWidget(self.pbPasteFromClipboard)
def onPasteFromClipboard(self):
if USE_PYPERCLIP:
self.lbOutput.setText(pyperclip.paste())
else:
clipboard = QApplication.clipboard()
self.lbOutput.setText(clipboard.text())
if __name__ == '__main__':
import sys
app = QApplication(sys.argv)
w = Widget()
w.show()
sys.exit(app.exec_())
from PySide.QtGui import *
from PySide.QtCore import *
import pyperclip
USE_PYPERCLIP = False
class Widget(QWidget):
def __init__(self, parent=None):
super(Widget, self).__init__(parent)
self.setupUi()
def setupUi(self):
self.setLayout(QVBoxLayout())
self.leInput = QLineEdit(self)
self.pbCopyToClipboard = QPushButton("Copy to clipboard", self)
self.pbCopyToClipboard.clicked.connect(self.onCopyToClipboard)
self.layout().addWidget(self.leInput)
self.layout().addWidget(self.pbCopyToClipboard)
def onCopyToClipboard(self):
if USE_PYPERCLIP:
pyperclip.copy(self.leInput.text())
else:
clipboard = QApplication.clipboard()
clipboard.setText(self.leInput.text())
if __name__ == '__main__':
import sys
app = QApplication(sys.argv)
w = Widget()
w.show()
sys.exit(app.exec_())
_______________________________________________
PySide mailing list
[email protected]
http://lists.qt-project.org/mailman/listinfo/pyside