Paste in the field of QTableView

 ̄綄美尐妖づ 提交于 2019-11-29 12:15:07

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!