6

pyqt:利用 QAbstractTableModel 生成了一个表,调用了一下 QAbstractItemModel 的 cu...

 2 years ago
source link: https://www.v2ex.com/t/835280
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

V2EX  ›  Python

pyqt:利用 QAbstractTableModel 生成了一个表,调用了一下 QAbstractItemModel 的 currentIndex,发现取不到第 0 行,咋回事?

  6167 · 1 天前 · 406 次点击

第 1 条附言  ·  1 天前

model如下:

class DictionaryTableModel(QtCore.QAbstractTableModel):
    def __init__(self, data, headers):
        super(DictionaryTableModel, self).__init__()
        self._data = data
        self._headers = headers

    def data(self, index, role):
        row = index.row()
        column = index.column()
        column_key = self._headers[column][0]  # [['字典key','释义'],...]
        value = self._data[row][column_key]
        if role == Qt.DisplayRole:
            return value

    def rowCount(self, index):
        # The length of the outer list.
        return len(self._data)

    def columnCount(self, index):
        # The length of our headers.
        return len(self._headers)

    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._headers[section][1])  # [['参数代码','释义'],...]

            if orientation == Qt.Vertical:
                return str(section + 1) + '    '

    def get_data_by_row(self, row):
        return self._data[row]
data = [
    {'VBELN': 11,'POSNR': 12},
    {'VBELN': 21,'POSNR': 22},
    {'VBELN': 31,'POSNR': 32}
]
headers = [['VBELN', '货号'], ['POSNR', '项目']]

self.model = DictionaryTableModel(data, headers)

self.tableView.setModel(self.model)

在点击表格行头后,一整行被选中,尝试使用self.tableView.currentIndex().row()获取行号时,选中第0行但是获取不到row,问题出在哪? 另外,在不选中任意一行的情况下,currentIndex().row()默认为最后一行,如何修改默认为None


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK