Qt QTableView内部的QComboBox组件,变化信号捕捉

如题,写Python 或 C++解答均可以,看得懂
大致代码是这样的

from PyQt5.QtWidgets import QComboBox, QTableView
from PyQt5.QtGui import QStandardItemModel, QStandardItem


class BaseTableView(QTableView):
    def __init__(self):
        super(BaseTableView, self).__init__()
        self.tmodel = QStandardItemModel(3, 3)
        self.setModel(self.tmodel)
        self.init_model()

    def init_model(self):
        for row in range(3):
            for col in range(3):
                if row == 0:
                    item = QStandardItem(str(col + 1))
                    self.tmodel.setItem(row, col, item)
                elif row == 1:
                    ans_box = QComboBox()
                    ans_box.addItems(["abc", "def", "ghi"])
                    self.setIndexWidget(self.tmodel.index(row, col), ans_box)
                else:
                    item = QStandardItem(str(col * 2))
                    self.tmodel.setItem(row, col, item)

我想做的就是当ans_box条目改变时(currentIndexChanged方法),能够获得改变的值且对应的ComboBox的位置,itemChanged方法验证无效果。需要怎么修改?
谢谢。


5-30 15:08更新 =>
自己想了一个办法,重写一个自定义ComboBox类,并另外设置index属性,再用self.sender.index得到行列序号,即:

class ItemCombo(QtWidgets.QComboBox):
    def __init__(self, func, index: tuple, items: list, conn=False):
        super(ItemCombo, self).__init__()
        self.func = func
        self.index = index
        self.addItems(items)
        if conn:
            self.conn_enabled()
        else:
            self.conn_disabled()

    def conn_enabled(self):
        self.currentIndexChanged.connect(self.func)

    def conn_disabled(self):
        try:
            self.currentIndexChanged.disconnect()
        except:
            pass

class CustomTableView(QTableView):
   ...
   def _inside_combo_changed(self, index): # 关联上方的self.func
       sender = self.sender()
       print(index, sender.index)

感觉我这个算是比较蠢的,不知道还有什么方法吗。学习学习


from PyQt5.QtWidgets import QComboBox, QTableView
from PyQt5.QtGui import QStandardItemModel, QStandardItem
from PyQt5.QtCore import pyqtSlot


class BaseTableView(QTableView):
    def __init__(self):
        super(BaseTableView, self).__init__()
        self.tmodel = QStandardItemModel(3, 3)
        self.setModel(self.tmodel)
        self.init_model()

    def init_model(self):
        for row in range(3):
            for col in range(3):
                if row == 0:
                    item = QStandardItem(str(col + 1))
                    self.tmodel.setItem(row, col, item)
                elif row == 1:
                    ans_box = QComboBox()
                    ans_box.addItems(["abc", "def", "ghi"])
                    ans_box.currentIndexChanged.connect(
                        lambda index, row=row, col=col: self.combobox_changed(index, row, col)
                    )
                    self.setIndexWidget(self.tmodel.index(row, col), ans_box)
                else:
                    item = QStandardItem(str(col * 2))
                    self.tmodel.setItem(row, col, item)

    @pyqtSlot(int, int, int)
    def combobox_changed(self, index, row, col):
        combobox = self.indexWidget(self.tmodel.index(row, col))
        selected_value = combobox.currentText()
        print("Changed value:", selected_value, "at position:", row, col)
        # 在这里执行您需要的操作


# 示例用法
if __name__ == "__main__":
    import sys
    from PyQt5.QtWidgets import QApplication

    app = QApplication(sys.argv)
    tableView = BaseTableView()
    tableView.show()
    sys.exit(app.exec_())