python - QTableView 选择更改
问题描述
在这个上搜索了一段时间,但我似乎找不到任何东西。在选择更改时需要 QTableView 的信号。试过tbl_view.itemSelectionChanged.connect(self.select_row)
但编译器抱怨这不存在。我还需要从选定的行中检索数据。有人可以指出我正确的方向吗?
解决方案
itemSelectionChanged
是一个QTableWidget
信号,因为在该类中存在 item 的概念,但在 QTableView 中不存在。如果是QTableView
,QListView
并且QTreeView
具有称为selectionModel()
返回跟踪所选元素的模型的方法,并且该模型在选择中有一个调用的信号selectionChanged()
,例如:例如:
from PyQt5 import QtCore, QtGui, QtWidgets
class MainWindow(QtWidgets.QMainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
table_view = QtWidgets.QTableView()
self.setCentralWidget(table_view)
model = QtGui.QStandardItemModel(5, 5, self)
table_view.setModel(model)
for i in range(model.rowCount()):
for j in range(model.columnCount()):
it = QtGui.QStandardItem(f"{i}-{j}")
model.setItem(i, j, it)
selection_model = table_view.selectionModel()
selection_model.selectionChanged.connect(self.on_selectionChanged)
@QtCore.pyqtSlot('QItemSelection', 'QItemSelection')
def on_selectionChanged(self, selected, deselected):
print("selected: ")
for ix in selected.indexes():
print(ix.data())
print("deselected: ")
for ix in deselected.indexes():
print(ix.data())
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
w = MainWindow()
w.show()
sys.exit(app.exec_())
推荐阅读
- java - 防止 AWS 实例停止
- java - Apache Commons 电子邮件设置(无法解析为类型),我似乎有正确的 JAR
- android - 如何编写 Robolectric 单元测试来测试回收者视图项目的向左滑动
- python-3.x - 实际交互与模拟 MockService 的预期交互不匹配
- browserstack - 如何使用 browserstack 的本地功能仅测试最新的浏览器版本?
- git - Git rebase 错误的提交
- python-3.x - 如何在 tkinter 标签中使用 png 图像获得平滑边缘
- kdb - 每个组的 KDB 顶行
- pip - 使用 pip 安装模块失败
- angular-material - 在角材料表中绑定动态数据