【问题标题】:looking for example for QCompleter with segmented completion / tree models寻找具有分段完成/树模型的 QCompleter 示例
【发布时间】:2021-10-12 07:41:49
【问题描述】:

PySide 文档在 QCompleter with tree models 上包含此部分:

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

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

为了使这种补全工作,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()

【问题讨论】:

  • 也许这个来自 Qt 文档的example 有帮助。
  • 我已经找到了,但我真的对如何制作树模型感到困惑。

标签: qt pyside qabstractitemmodel qcompleter


【解决方案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_()

作品:

作为树模型:

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

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

【讨论】:

  • 谢谢...问题不仅在于 QCompleter,还在于如何制作树模型本身。 (QFileSystemModel 很棒,但它不能帮助我以与 QCompleter 兼容的方式封装我的分层数据)
【解决方案2】:

我找不到我想要的好例子,但我想出了如何调整 Qt TreeModel 示例以使用 QCompleter:

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

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)    

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-11-19
    • 2017-05-20
    • 2020-09-27
    • 2016-01-31
    • 1970-01-01
    • 2023-03-06
    • 2022-01-01
    相关资源
    最近更新 更多