Python# -*- coding: cp1251 -*-#file aaa.py import sysfrom PyQt4 import QtCore, QtGui class _MimeData(QtCore.QMimeData): u"Класс поддержки клипбоарда" format_templ = u'application/x-python-mime;value="%s"' obj = None format_name = '' def __init__(self, obj): super(_MimeData, self).__init__() self.obj = obj self.format_name = self.format_templ % obj.__class__.__name__ def hasFormat(self, mimetype): if mimetype in (self.format_name, u'text/plain'): return True return QtCore.QMimeData.hasFormat(self, mimetype) def formats(self): base_formats = QtCore.QMimeData.formats(self) return list(set(list(base_formats) + [self.format_name, u'text/plain'])) def retrieveData(self, mimetype, preferredType): print 'retrieveData(%s, %s)' %(mimetype, preferredType) if mimetype == self.format_name: return QtCore.QVariant(self.obj) if mimetype == u'text/plain': return QtCore.QVariant(u'%s' % self.obj) return QtCore.QMimeData.retrieveData(self, mimetype, preferredType) def main(): app = QtGui.QApplication(sys.argv) print 'copy data to clipboard' data1 = _MimeData(['aaa', 'bbb', 'ccc']) QtGui.QApplication.clipboard().setMimeData(data1) print 'paste data from clipboard' data2 = QtGui.QApplication.clipboard().mimeData() text = data2.text() print text if __name__ == '__main__': main()
C:\Lang>aaa.pycopy data to clipboardpaste data from clipboardretrieveData(text/plain, 10)['aaa', 'bbb', 'ccc']
Pythonwidget = QtGui.QTextEdit()widget.show()app.exec_()
C:\Lang\test\python\qtClipb>aaa.py2008-12-01 14:04:22.971000 copy data to clipboard!>2008-12-01 14:04:29.148000 retrieveData(text/plain, 10)2008-12-01 14:04:34.668000 paste data from clipboard2008-12-01 14:04:34.668000 retrieveData(text/plain, 10)['aaa', 'bbb', 'ccc']
C:\Lang\test\python\qtClipb>bbb.py2008-12-01 14:04:29.123000 paste data from clipboard2008-12-01 14:04:29.150000 ['aaa', 'bbb', 'ccc']
Python# -*- coding: cp1251 -*-#file aaa.pyimport sysfrom datetime import datetimefrom PyQt4 import QtCore, QtGui #Работа с клипбоардом.class _MimeData(QtCore.QMimeData): u"Класс поддержки клипбоарда" format_templ = u'application/x-python-mime;value="%s"' obj = None format_name = '' def __init__(self, obj): super(_MimeData, self).__init__() self.obj = obj self.format_name = self.format_templ % obj.__class__.__name__ def hasFormat(self, mimetype): if mimetype in (self.format_name, u'text/plain'): return True return QtCore.QMimeData.hasFormat(self, mimetype) def formats(self): base_formats = QtCore.QMimeData.formats(self) return list(set(list(base_formats) + [self.format_name, u'text/plain'])) def retrieveData(self, mimetype, preferredType): print '%s retrieveData(%s, %s)' %(datetime.now(), mimetype, preferredType) if mimetype == self.format_name: return QtCore.QVariant(self.obj) if mimetype == u'text/plain': return QtCore.QVariant(u'%s' % self.obj) return QtCore.QMimeData.retrieveData(self, mimetype, preferredType) def main(): app = QtGui.QApplication(sys.argv) print datetime.now(), 'copy data to clipboard' data1 = _MimeData(['aaa', 'bbb', 'ccc']) QtGui.QApplication.clipboard().setMimeData(data1) raw_input('!>') print datetime.now(), 'paste data from clipboard' data2 = QtGui.QApplication.clipboard().mimeData() text = data2.text() print text if __name__ == '__main__': main()
Python# -*- coding: cp1251 -*-#file bbb.pyimport sysfrom datetime import datetimefrom PyQt4 import QtCore, QtGui def main(): app = QtGui.QApplication(sys.argv) print datetime.now(), 'paste data from clipboard' data2 = QtGui.QApplication.clipboard().mimeData() text = data2.text() print datetime.now(), text if __name__ == '__main__': main()