一半君的总结纸

听话只听一半君

QComboBox加自动完成(不是默认的从字符串首开始match)的疑问?

小弟我有一个QComboBox想要自动完成,所以加了个QCompleter,但是发现只能从字符串开头开始match,比如 chicken rice, steam rice, rice cooker, 如果我输rice进去 只会有第三项出现,遂从stackoverflow搜了个方法 ,但是不知道为什么只在第一次弹出 下拉备选列表时有效,第二次下拉列表就看不到了(还是有效,只是下拉菜单好像出现在了后面,按上下键还是可以选的)

,直到我加上了

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

完全不知道是为什么 瞎猫撞到死耗子试出来了,求解为啥非要加这个.

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 = ['Jony_walk_cycleData','Jane_walk','Albert_walkSlowly', 'Alfred_running','Jane_running','Jane_jogging','Albert_lookingAround']
        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)

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

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

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

        self.connect(self.combobox,SIGNAL("activated(int)"),self.selectFromCombobox)
        self.connect(completer.popup(), SIGNAL("clicked(QModelIndex)"),lambda i:self.selectFromCompleter(self.combobox,i))

    def selectFromCombobox(self,index):
        print 'set cycleName from combobox activated...'
        combobox = self.sender()
        print index
        combobox.setCurrentIndex(index)
#        combobox.setEditable(False)

    def selectFromCompleter(self,combobox,index):
        print "set cycleName from completer clicked"
        listView = self.sender()
        print 'new cycle name :',index.model().data(index).toString()
        newIndex = combobox.findText(index.model().data(index).toString())
        combobox.setCurrentIndex(newIndex)
#        combobox.setEditable(False)

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()

        # cr = QRect( QPoint(100, 200), QSize(11, 16)) ;
        # cr.setWidth(self.popup().sizeHintForColumn(0)
        # + self.popup().verticalScrollBar().sizeHint().width())

        cr=QRect(QPoint(1, 1), QSize(1, 1))
        self.complete(cr)
        # self.popup().show()
        # self.popup().raise_()

    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_())

Advertisements

发表评论

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / 更改 )

Twitter picture

You are commenting using your Twitter account. Log Out / 更改 )

Facebook photo

You are commenting using your Facebook account. Log Out / 更改 )

Google+ photo

You are commenting using your Google+ account. Log Out / 更改 )

Connecting to %s

%d 博主赞过: