寻找具有分段完成/树模型的QCompleter的示例

时间:2014-07-10 22:54:21

标签: qt pyside qabstractitemmodel qcompleter

PySide文档包含QCompleter with tree models上的这一部分:

  

PySide.QtGui.QCompleter可以在树模型中查找完成,假设任何项(或子项或子子项)都可以通过指定项的路径明确地表示为字符串。然后一次完成一个级别的完成。

     

我们以用户输入文件系统路径为例。该模型是(分层的)PySide.QtGui.QFileSystemModel。路径中的每个元素都会完成。例如,如果当前文本是C:\ Wind,PySide.QtGui.QCompleter可能会建议Windows完成当前路径元素。同样,如果当前文本是C:\ Windows \ Sy,PySide.QtGui.QCompleter可能会建议系统。

     

为了使这种完成工作,PySide.QtGui.QCompleter需要能够将路径拆分为在每个级别匹配的字符串列表。对于C:\ Windows \ Sy,需要将其拆分为“C:”,“Windows”和“Sy”。 PySide.QtGui.QCompleter.splitPath()的默认实现,如果模型是PySide.QtGui.QFileSystemModel,则使用QDir.separator()拆分PySide.QtGui.QCompleter.completionPrefix()。

     

要提供完成,PySide.QtGui.QCompleter需要知道索引的路径。这是由PySide.QtGui.QCompleter.pathFromIndex()提供的。 PySide.QtGui.QCompleter.pathFromIndex()的默认实现返回列表模型的编辑角色的数据,如果模式是PySide.QtGui.QFileSystemModel,则返回绝对文件路径。

但我似乎无法找到一个展示如何做到这一点的例子。 任何人都可以指出一个我可以作为起点的例子吗?(在我的调查中看起来似乎很难是树模型而不是QCompleter)

看起来你需要提供这些功能:

  • 将字符串拆分为细分的功能(对于给定的示例,C:\Windows\Sy['C:','Windows','Sy']
  • 指定包含最后一个细分的商品列表的功能(例如['C:','Windows']
  • 中包含的所有商品

我找到了一个关于QCompleter基本功能的例子,并且能够很好地调整基础知识(见下文),我只是不知道如何实现树模型类型的应用程序。

'''based on
http://codeprogress.com/python/libraries/pyqt/showPyQTExample.php?index=403&key=QCompleterQLineEdit'''

from PySide.QtGui import * 
from PySide.QtCore import * 
import sys

def main():    
    app     = QApplication(sys.argv)
    edit     = QLineEdit()
    strList     = '''
Germany;Russia;France;
french fries;frizzy hair;fennel;fuzzball
frayed;fickle;Frobozz;fear;framing;frames
Franco-American;Frames;fancy;fire;frozen yogurt
football;fnord;foul;fowl;foo;bar;baz;quux
family;Fozzie Bear;flinch;fizzy;famous;fellow
friend;fog;foil;far;flower;flour;Florida
'''.replace('\n',';').split(";")
    strList.sort(key=lambda s: s.lower())
    completer     = QCompleter(strList,edit)
    completer.setCaseSensitivity(Qt.CaseInsensitive)

    edit.setWindowTitle("PySide QLineEdit Auto Complete")    
    edit.setCompleter(completer)
    edit.show()

    sys.exit(app.exec_())

if __name__ == '__main__':
    main()

2 个答案:

答案 0 :(得分:5)

我无法找到我想要的好例子,但我想出了如何使Qt TreeModel示例适应使用QCompleter:

https://gist.github.com/jason-s/9dcef741288b6509d362

enter image description here

QCompleter是一个简单的部分,您只需告诉它如何将路径分割成段,然后如何从模型中的特定条目返回到路径:

class MyCompleter(QtGui.QCompleter):
    def splitPath(self, path):
        return path.split('/')
    def pathFromIndex(self, index):
        result = []
        while index.isValid():
            result = [self.model().data(index, QtCore.Qt.DisplayRole)] + result
            index = index.parent()
        r = '/'.join(result)
        return r

除此之外,您必须正确配置QCompleter,告诉它如何从模型项到文本字符串。在这里,我将其设置为使用DisplayRole并使用第0列。

edit     = QtGui.QLineEdit()
completer     = MyCompleter(edit)
completer.setModel(model)
completer.setCompletionColumn(0)
completer.setCompletionRole(QtCore.Qt.DisplayRole)
completer.setCaseSensitivity(QtCore.Qt.CaseInsensitive)    

答案 1 :(得分:1)

正如QCompleter的文档所说,您可以提供两种模型:列表模型或树模型。

列表模型的示例,在您的示例之后:

from PySide import QtGui

app = QtGui.QApplication([])

edit = QtGui.QLineEdit()
strList = "Germany;Russia;France;Norway".split(";")
completer = QtGui.QCompleter(strList)
edit.setCompleter(completer)
edit.show()

app.exec_()

工作的:

enter image description here

作为树模型:

from PySide import QtGui, QtCore

app = QtGui.QApplication([])

edit = QtGui.QLineEdit()
model = QtGui.QFileSystemModel()
model.setFilter(QtCore.QDir.AllDirs | QtCore.QDir.Drives)
model.setRootPath('')
completer = QtGui.QCompleter(model, edit)
edit.setCompleter(completer)
edit.show()

app.exec_()

由于一些奇怪的原因,这里没有显示任何内容。稍后会调查。