V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
6167
V2EX  ›  Python

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

  •  
  •   6167 · 2022-02-21 00:40:44 +08:00 · 2441 次点击
    这是一个创建于 767 天前的主题,其中的信息可能已经有所发展或是发生改变。
    第 1 条附言  ·  2022-02-21 01:50:06 +08:00

    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

    3 条回复    2022-02-24 20:03:18 +08:00
    justou
        1
    justou  
       2022-02-21 09:11:35 +08:00
    你的代码不全, 应该贴一个完整的 demo, 拿来就能 debug 那种

    1. 一整行被选中,尝试使用 self.tableView.currentIndex().row()获取行号应该是在一个 slot 中, 你是怎么写的?
    2. 在不选中任意一行的情况下, currentIndex 是 invalid 的, row()返回-1 不是表示最后一行, 行索引的有效范围是[0, rowCount() - 1], 在处理一个 index 时注意使用 isValid()判断
    6167
        2
    6167  
    OP
       2022-02-21 16:33:59 +08:00
    @justou 感谢提示,但是刚刚在整理代码的时候,突然发现昨天晚上的 bug 复现不出来了,currentIndex().row()也默认为-1 了
    imn1
        3
    imn1  
       2022-02-24 20:03:18 +08:00
    表格的数据行的下标从 0 开始,标题行不计入
    标题行是另一个"控件",有其自有的方法属性,要用 view.header()类似的方法提取这个控件

    currentIndex 仅指光标当前的行,跟选择无直接关系,所以行号就是其实际值
    你想获得选择的行(可以是多行)的第一个,不是用 currentIndex ,而是用 selectedIndexes/selectedItems 类似的方法

    提醒一下,无论光标还是选择,行号都是该行的真实行号,并非多个选择中的排第几个
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1026 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 36ms · UTC 22:21 · PVG 06:21 · LAX 15:21 · JFK 18:21
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.