当我选择下拉式自动完成项时,我想得到存储在[(1, 'cb'), (3, 'cd'), (7, 'ca'), (11, 'aa'), (22, 'bd')]
中的整数。在
因为我使用了QSortFilterProxyModel,所以当使用down键选择项时,索引来自代理模型。在
我在文档中读到应该使用mapToSource
来获得原始模型中的索引,但是这里我得到了一个错误消息index from wrong model passed to mapToSource
,并且{
错误是:
row in proxy model 0
QSortFilterProxyModel: index from wrong model passed to mapToSource
row in original model -1
代码:
^{pr2}$更新: 这个问题已经解决了,感谢来自pyqt的Avaris的帮助。结果我可以这样做,将索引映射到原始模型
proxy_index= self.completer.completionModel().mapToSource(index)
print 'original row:', self.completer.model().mapToSource(proxy_index).row()
甚至更好:
print 'data:', index.data(Qt.UserRole).toPyObject()
因为:“ completionModel()实际上是.model()上的代理模型
你不需要为此而搅乱mapToSource。索引数据(Qt.UserRole)不管返回的是哪个索引,都应该给出该数字
仅供参考,您很少需要在(代理)模型之外使用mapToSource。主要是内部使用。一个合适的代理应该转发来自源的所有相关查询。所以你可以像使用源代理一样使用代理 -阿瓦里斯 ““
请在此处粘贴正确的代码以供参考
相关问题 更多 >
编程相关推荐