You can obtain the clipboard form the QApplication
instance of your app using QApplication.clipboard()
, and from the QClipboard
object returned you can get the text, image, mime data, etc. Here is an example:
import PyQt4.QtGui as gui
class Widget(gui.QWidget):
def __init__(self,parent=None):
gui.QWidget.__init__(self,parent)
# initially construct the visible table
self.tv=gui.QTableWidget()
self.tv.setRowCount(1)
self.tv.setColumnCount(1)
self.tv.show()
# set the shortcut ctrl+v for paste
gui.QShortcut(gui.QKeySequence('Ctrl+v'),self).activated.connect(self._handlePaste)
self.layout = gui.QVBoxLayout(self)
self.layout.addWidget(self.tv)
# paste the value
def _handlePaste(self):
clipboard_text = gui.QApplication.instance().clipboard().text()
item = gui.QTableWidgetItem()
item.setText(clipboard_text)
self.tv.setItem(0, 0, item)
print clipboard_text
app = gui.QApplication([])
w = Widget()
w.show()
app.exec_()
Note: I've used a QTableWidget
cause I don't have a model to use with QTableView
but you can adapt the example to your needs.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…