2013-02-07 46 views
0

例如,我在我的QComboBox下拉列表如何使用QSortFilterProxyModel自定義QCompleter完成規則?

chicken soup 
chilli peppers 
grilled chicken 

進入「CH」將匹配「雞湯」和「辣椒」,而不是「烤雞」這三個項目。

我想要的是能夠輸入「ch」並匹配所有這些或更具體地說,「雞」,並匹配「雞湯」和「烤雞肉」。

參考: https://bugreports.qt-project.org/browse/QTBUG-7830

下面的代碼工作只有一次,爲什麼呢? 如果輸入'a',所有包含'a'的字符串都顯示正確,則返回3個結果,'Jane Doe','Albert Einstein','Alfred E Newman' 但如果繼續輸入'n'沒有結果顯示在下拉菜單中。

編輯:它的工作原理,但無法正確顯示彈出而QListView(如果按向下箭頭鍵,符合條件的商品將在下拉列表編輯區顯示,但下拉列表中未顯示)

from PyQt4.QtCore import * 
from PyQt4.QtGui import * 

from PyQt4 import QtGui,QtCore 

import sys 

import signal 
signal.signal(signal.SIGINT, signal.SIG_DFL) 


class MyGui(QDialog): 

    def __init__(self, parent=None): 

     super(MyGui,self).__init__(parent) 

     model = QtGui.QStringListModel() 
     wordList = ['John Doe','Jane Doe','Albert Einstein', 'Alfred E Newman'] 
     model.setStringList(wordList) 

     layout = QtGui.QVBoxLayout(self) 
     self.line = QtGui.QLineEdit(self) 
     layout.addWidget(self.line) 

     self.combobox = QComboBox(parent) 
     layout.addWidget(self.combobox) 
     self.combobox.addItems(wordList) 
     self.combobox.setEditable(True) 

     self.setLayout(layout) 

     complete = CustomQCompleter2(self) 
     complete.setModel(model) 
     complete.setCaseSensitivity(QtCore.Qt.CaseInsensitive) 
     complete.setCompletionMode(QCompleter.UnfilteredPopupCompletion) 
     complete.setWrapAround(False) 

     self.line.setCompleter(complete) 
     self.combobox.setCompleter(complete) 
     self.combobox.setEditText('') 

     self.setGeometry(200, 100, 400, 300) 


class CustomQCompleter2(QCompleter): 
    def __init__(self, parent=None): 
     super(CustomQCompleter2, self).__init__(parent) 
     self.local_completion_prefix = "" 
     self.source_model = None 

    def setModel(self, model): 
     self.source_model = model 
     super(CustomQCompleter2, self).setModel(self.source_model) 

    def updateModel(self): 
     local_completion_prefix = self.local_completion_prefix 
     class InnerProxyModel(QSortFilterProxyModel): 
      def filterAcceptsRow(self, sourceRow, sourceParent): 
       index0 = self.sourceModel().index(sourceRow, 0, sourceParent) 
       searchStr = local_completion_prefix.lower() 
       modelStr = self.sourceModel().data(index0,Qt.DisplayRole).toString().toLower() 
       print searchStr,' in ',modelStr, searchStr in modelStr 
       return searchStr in modelStr 


     proxy_model = InnerProxyModel() 

     proxy_model.setSourceModel(self.source_model) 

     super(CustomQCompleter2, self).setModel(proxy_model) 
     print 'match :',proxy_model.rowCount() 


    def splitPath(self, path): 
     self.local_completion_prefix = str(path) 
     self.updateModel() 
     return "" 

if __name__ == '__main__': 

    app = QtGui.QApplication(sys.argv) 
    gui = MyGui() 
    gui.show() 
    sys.exit(app.exec_()) 
+0

這段代碼對我來說工作正常。輸入'an'可以打開3個選項。 – Avaris

回答

1

將下面的代碼添加到updateModel的結尾解決問題的方法,但我不知道它爲什麼起作用。

cr=QRect(QPoint(1, 1), QSize(1, 1)) 
self.complete(cr)