在 QTableWidget 单元格 PyQt5 的标签中复制粘贴文本
copy paste text in label in QTableWidget cell PyQt5
我的应用程序中有多个 QTableWidget:
我希望能够通过按 ctrl+c 或右键单击单元格并单击 'copy' 菜单来复制存储在单元格中的文本。
我使用的表是costumized,这是实现em的代码:
class AlphaPiQTableWidget(QTableWidget):
def __init__(self, attr_dict):
super(AlphaPiQTableWidget, self).__init__()
values = list(attr_dict.values())
attributes = list(attr_dict.keys())
# Tabella che mostra i valori del competitor
self.setRowCount(len(attributes))
self.setColumnCount(2)
self.setCellWidget(0,0, QLabel(attributes[0]))
self.setCellWidget(1,0, QLabel(attributes[1]))
self.setCellWidget(2,0, QLabel(attributes[2]))
self.setCellWidget(3,0, QLabel(attributes[3]))
self.setCellWidget(0,1, QLabel(str(values[0])))
self.setCellWidget(1,1, QLabel(str(values[1])))
self.setCellWidget(2,1, QLabel(str(values[2])))
self.setCellWidget(3,1, QLabel(str(values[3])))
self.setContextMenuPolicy(QtCore.Qt.CustomContextMenu)
self.customContextMenuRequested.connect(self.showTableMenu)
# Fai occupare tutto lo spazio libero nel layout alla tabella
h_header = self.horizontalHeader()
h_header.setSectionResizeMode(QtWidgets.QHeaderView.Stretch)
h_header.setVisible(False)
v_header = self.verticalHeader()
v_header.setSectionResizeMode(QtWidgets.QHeaderView.Stretch)
v_header.setVisible(False)
# Fai in modo che la tabella sia sempre visualizzata per intero
self.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
self.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
def showTableMenu(self, pos):
# get the text of the index at the mouse cursor (if any)
text = self.indexAt(pos).data()
menu = QtWidgets.QMenu()
copyAction = menu.addAction('Copy')
if not text:
copyAction.setEnabled(False)
# show the menu
res = menu.exec_(QtGui.QCursor.pos())
if res == copyAction:
# if the menu has been triggered by the action, copy to the clipboard
QtWidgets.QApplication.clipboard().setText(text)
问题是,当我通过右键单击单元格并按 'copy' self.indexAt(pos).data()
来复制文本时,总是 returns None
。这是为什么?
P.S。所有 AlphaPiQTableWidget
都与其他小部件(如您所见的标签和按钮)一起存储在 QHBoxLayout 中。
有单元格并不意味着有项目,在您的情况下,您已将小部件放置在单元格之上,因此实际上您想要获取这些 QLabel 的文本。
首先是通过cellWidget方法获取QLabel,但是需要获取按下的cell的行和列,此时必须使用indexAt()方法:
def showTableMenu(self, pos):
index = self.indexAt(pos)
label = self.cellWidget(index.row(), index.column())
text = label.text()
# ...
另一种解决方案是不使用 QLabel,而是使用 QTableWidgetItem:
self.<b>setItem(0,0, QTableWidgetItem</b>(attributes[0]))
# ...
我的应用程序中有多个 QTableWidget:
我希望能够通过按 ctrl+c 或右键单击单元格并单击 'copy' 菜单来复制存储在单元格中的文本。
我使用的表是costumized,这是实现em的代码:
class AlphaPiQTableWidget(QTableWidget):
def __init__(self, attr_dict):
super(AlphaPiQTableWidget, self).__init__()
values = list(attr_dict.values())
attributes = list(attr_dict.keys())
# Tabella che mostra i valori del competitor
self.setRowCount(len(attributes))
self.setColumnCount(2)
self.setCellWidget(0,0, QLabel(attributes[0]))
self.setCellWidget(1,0, QLabel(attributes[1]))
self.setCellWidget(2,0, QLabel(attributes[2]))
self.setCellWidget(3,0, QLabel(attributes[3]))
self.setCellWidget(0,1, QLabel(str(values[0])))
self.setCellWidget(1,1, QLabel(str(values[1])))
self.setCellWidget(2,1, QLabel(str(values[2])))
self.setCellWidget(3,1, QLabel(str(values[3])))
self.setContextMenuPolicy(QtCore.Qt.CustomContextMenu)
self.customContextMenuRequested.connect(self.showTableMenu)
# Fai occupare tutto lo spazio libero nel layout alla tabella
h_header = self.horizontalHeader()
h_header.setSectionResizeMode(QtWidgets.QHeaderView.Stretch)
h_header.setVisible(False)
v_header = self.verticalHeader()
v_header.setSectionResizeMode(QtWidgets.QHeaderView.Stretch)
v_header.setVisible(False)
# Fai in modo che la tabella sia sempre visualizzata per intero
self.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
self.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
def showTableMenu(self, pos):
# get the text of the index at the mouse cursor (if any)
text = self.indexAt(pos).data()
menu = QtWidgets.QMenu()
copyAction = menu.addAction('Copy')
if not text:
copyAction.setEnabled(False)
# show the menu
res = menu.exec_(QtGui.QCursor.pos())
if res == copyAction:
# if the menu has been triggered by the action, copy to the clipboard
QtWidgets.QApplication.clipboard().setText(text)
问题是,当我通过右键单击单元格并按 'copy' self.indexAt(pos).data()
来复制文本时,总是 returns None
。这是为什么?
P.S。所有 AlphaPiQTableWidget
都与其他小部件(如您所见的标签和按钮)一起存储在 QHBoxLayout 中。
有单元格并不意味着有项目,在您的情况下,您已将小部件放置在单元格之上,因此实际上您想要获取这些 QLabel 的文本。
首先是通过cellWidget方法获取QLabel,但是需要获取按下的cell的行和列,此时必须使用indexAt()方法:
def showTableMenu(self, pos):
index = self.indexAt(pos)
label = self.cellWidget(index.row(), index.column())
text = label.text()
# ...
另一种解决方案是不使用 QLabel,而是使用 QTableWidgetItem:
self.<b>setItem(0,0, QTableWidgetItem</b>(attributes[0]))
# ...