How to get QTableView right clicked index(如何获取 QTableView 右键单击索引)
本文介绍了如何获取 QTableView 右键单击索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
下面的代码创建了一个带有 QTableView
视图的对话框.左键单击 onLeftClick
函数会获得一个 QModelIndex index
.此 QModelIndex 稍后用于打印左键单击单元格的行号和列号.
The code below creates a single dialog with a QTableView
view.
On left-click the onLeftClick
function gets an QModelIndex index
.
This QModelIndex is used later to print the row and column numbers of the left-clicked cell.
如何获取被右键单击的单元格的QModelIndex
索引?
How to get the QModelIndex
index of the cell that was right-clicked?
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
app = QApplication([])
class Dialog(QDialog):
def __init__(self, parent=None):
super(Dialog, self).__init__(parent)
self.setLayout(QVBoxLayout())
self.view = QTableView(self)
self.view.setSelectionBehavior(QTableWidget.SelectRows)
self.view.setContextMenuPolicy(Qt.CustomContextMenu)
self.view.customContextMenuRequested.connect(self.onRightClick)
self.view.clicked.connect(self.onLeftClick)
self.view.setModel(QStandardItemModel(4, 4))
for each in [(row, col, QStandardItem('item %s_%s' % (row, col))) for row in range(4) for col in range(4)]:
self.view.model().setItem(*each)
self.layout().addWidget(self.view)
self.resize(500, 250)
self.show()
def onRightClick(self, qPoint):
sender = self.sender()
for index in self.view.selectedIndexes():
print 'onRightClick selected index.row: %s, selected index.column: %s' % (index.row(), index.column())
def onLeftClick(self, index):
print 'onClick index.row: %s, index.row: %s' % (index.row(), index.column())
dialog = Dialog()
app.exec_()
推荐答案
你必须使用 QAbstractScrollArea
(QTableView
的 indexAt()
方法代码>):
You have to use the indexAt()
method of the QAbstractScrollArea
(QTableView
):
def onRightClick(self, qPoint):
index = self.view.indexAt(qPoint)
if index.isValid():
print('onClick index.row: %s, index.col: %s' % (index.row(), index.column()))
这篇关于如何获取 QTableView 右键单击索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
沃梦达教程
本文标题为:如何获取 QTableView 右键单击索引


猜你喜欢
- 检查具有纬度和经度的地理点是否在 shapefile 中 2022-01-01
- ";find_element_by_name(';name';)";和&QOOT;FIND_ELEMENT(BY NAME,';NAME';)";之间有什么区别? 2022-01-01
- 我如何卸载 PyTorch? 2022-01-01
- CTR 中的 AES 如何用于 Python 和 PyCrypto? 2022-01-01
- 如何使用PYSPARK从Spark获得批次行 2022-01-01
- YouTube API v3 返回截断的观看记录 2022-01-01
- 我如何透明地重定向一个Python导入? 2022-01-01
- 计算测试数量的Python单元测试 2022-01-01
- 使用公司代理使Python3.x Slack(松弛客户端) 2022-01-01
- 使用 Cython 将 Python 链接到共享库 2022-01-01