QTableView 选择改变时的触发动作

问题描述

我试图在 QTableView 的单元格选择时触发操作。我正在使用 selectionChanged 但它仅在第一次触发操作并且更改单元格选择不会再次触发该操作。我做错了什么。

import sys
from PySide6 import QtCore,QtGui,QtWidgets
from PySide6.QtCore import Qt
import pandas as pd


class TableModel(QtCore.QAbstractTableModel):
def __init__(self,data):
    super(TableModel,self).__init__()
    self._data = data

def data(self,index,role):
    if role == Qt.displayRole:
        value = self._data.iloc[index.row(),index.column()]
        return str(value)

def rowCount(self,index):
    return self._data.shape[0]

def columnCount(self,index):
    return self._data.shape[1]

def headerData(self,section,orientation,role):
    # section is the index of the column/row.
    if role == Qt.displayRole:
        if orientation == Qt.Horizontal:
            return str(self._data.columns[section])

        if orientation == Qt.Vertical:
            return str(self._data.index[section])


class MainWindow(QtWidgets.QMainWindow):

def __init__(self):
    super().__init__()

    self.table = QtWidgets.QTableView()

    data = pd.DataFrame([
      [1,9,2],[1,-1],[3,5,3,[5,8,9],],columns = ['A','B','C'],index=['Row 1','Row 2','Row 3','Row 4','Row 5'])

    self.model = TableModel(data)
    self.table.setModel(self.model)
    selectionModel = self.table.selectionModel()
    selectionModel.selectionChanged.connect(self.printLocation())
    self.setCentralWidget(self.table)

def printLocation(self):
    print(self.table.currentIndex())

app=QtWidgets.QApplication(sys.argv)
window=MainWindow()
window.show()
app.exec_()

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)