Python 如何使用QTableView获取复制粘贴数据
用户希望在外部文本编辑器中复制文本,并使用Mac上的Python 如何使用QTableView获取复制粘贴数据,python,qt,pyqt,qt5,pyqt5,Python,Qt,Pyqt,Qt5,Pyqt5,用户希望在外部文本编辑器中复制文本,并使用Mac上的Command+v或“Control+v”热键将其粘贴到QTableView 我已经实现了keyPressEvent,它跟踪每个用户的键盘操作。但是,如何从剪贴板中获取数据呢 from PyQt5.QtGui import * from PyQt5.QtWidgets import * from PyQt5.QtCore import * class View(QTableView): def __init__(self):
Command+v
或“Control+v”热键将其粘贴到QTableView
我已经实现了keyPressEvent
,它跟踪每个用户的键盘操作。但是,如何从剪贴板中获取数据呢
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
class View(QTableView):
def __init__(self):
super(View, self).__init__(parent=None)
def keyPressEvent(self, event):
if event.matches(QKeySequence.Copy):
print 'Ctrl + C'
if event.matches(QKeySequence.Paste):
print 'Ctrl + V'
QTableView.keyPressEvent(self, event)
app = QApplication([])
view = View()
view.show()
qApp.exec_()
您必须使用类的对象,在这种情况下,我们通过
QApplication
获取它,以获取我们使用的文本并粘贴我们使用的文本
就你而言:
def keyPressEvent(self, event):
clipboard = QApplication.clipboard()
if event.matches(QKeySequence.Copy):
print('Ctrl + C')
clipboard.setText("some text")
if event.matches(QKeySequence.Paste):
print(clipboard.text())
print('Ctrl + V')
QTableView.keyPressEvent(self, event)
您必须使用类的对象,在本例中,我们通过
QApplication
获取它,以获取我们使用的文本并粘贴我们使用的文本
就你而言:
def keyPressEvent(self, event):
clipboard = QApplication.clipboard()
if event.matches(QKeySequence.Copy):
print('Ctrl + C')
clipboard.setText("some text")
if event.matches(QKeySequence.Paste):
print(clipboard.text())
print('Ctrl + V')
QTableView.keyPressEvent(self, event)
如果希望在运行Ctrl+V时创建项目?,则不会自动执行此操作,因此必须通过模型执行此操作。如果将文本粘贴到什么位置,因为它是QTableView?打印粘贴的文本或数据在本例中就可以了。如果希望在运行Ctrl+V时创建项目,则不会自动执行此操作,因此必须通过模型执行此操作。如果粘贴文本,由于它是QTableView,您希望它粘贴在什么位置?对于本例,打印粘贴的文本或数据就可以了。