gpt4 book ai didi

python - 粘贴到QTableView的字段中

转载 作者:行者123 更新时间:2023-11-28 22:51:15 24 4
gpt4 key购买 nike

我需要在 python 中实现一个函数,该函数在按下“ctrl+v”时处理“粘贴”。我有一个QTableView,我需要复制表格的一个字段并将其粘贴到表格的另一个字段。我试过下面的代码,但问题是我不知道如何在 tableView 中读取复制的项目(从剪贴板)。 (因为它已经复制了该字段,我可以像记事本一样将其粘贴到其他任何地方)。这是我试过的部分代码:

class Widget(QWidget):
def __init__(self,md,parent=None):
QWidget.__init__(self,parent)
# initially construct the visible table
self.tv=QTableView()
self.tv.show()

# set the shortcut ctrl+v for paste
QShortcut(QKeySequence('Ctrl+v'),self).activated.connect(self._handlePaste)

self.layout = QVBoxLayout(self)
self.layout.addWidget(self.tv)

# paste the value
def _handlePaste(self):
if self.tv.copiedItem.isEmpty():
return
stream = QDataStream(self.tv.copiedItem, QIODevice.ReadOnly)
self.tv.readItemFromStream(stream, self.pasteOffset)

最佳答案

您可以使用 QApplication.clipboard() 从应用程序的 QApplication 实例中获取剪贴板,并从返回给您的 QClipboard 对象中获取剪贴板可以获取文本、图片、mime数据等。下面是一个例子:

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_()

注意:我使用了 QTableWidget,因为我没有可与 QTableView 一起使用的模型,但您可以根据需要调整示例。

关于python - 粘贴到QTableView的字段中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21682261/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com