Pass QModelIndex instead of QString when QCompleter highlighted

只谈情不闲聊 提交于 2020-01-17 00:34:46

问题


There is a QCompleter (set to QLineEdit) populated with QStandardItemModel. That model also populates the QTableView, I need to get the QModelIndex and select it in QTableView but it fails, it passes text instead of QModelIndex:

completer.highlighted.connect(print_index) 

passes only the first index:

completer.highlighted.connect(lambda : select_index(completer.currentIndex()))

def select_index(index):

    table_view.setCurrentIndex(index)

I read docs, but cannot understand what do I do wrong. http://doc.qt.io/qt-5/qcompleter.html#highlighted-1


回答1:


There's two versions of the highlighted signal: the default one emits a string, the other emits a QModelIndex To get the index, use:

completer.highlighted[QtCore.QModelIndex].connect(onHighlight)

But be careful, this is the index in the completion model, not the model you populated the completer with. You can use mapToSource to get the original index.

def onHighLight(index):
    #completer model
    print(index)
    #model 
    sourceIndex=completer.completionModel().mapToSource(index)
    print(sourceIndex)



回答2:


I would like to use row() function of QmodelIndex. It will directly return the list index of your current selection.



来源:https://stackoverflow.com/questions/34237970/pass-qmodelindex-instead-of-qstring-when-qcompleter-highlighted

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!