gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 05:25:31 34 4
gpt4 key购买 nike

PySide 文档包含关于 QCompleter with tree models 的此部分:

PySide.QtGui.QCompleter can look for completions in tree models, assuming that any item (or sub-item or sub-sub-item) can be unambiguously represented as a string by specifying the path to the item. The completion is then performed one level at a time.

Let’s take the example of a user typing in a file system path. The model is a (hierarchical) PySide.QtGui.QFileSystemModel . The completion occurs for every element in the path. For example, if the current text is C:\Wind , PySide.QtGui.QCompleter might suggest Windows to complete the current path element. Similarly, if the current text is C:\Windows\Sy , PySide.QtGui.QCompleter might suggest System .

For this kind of completion to work, PySide.QtGui.QCompleter needs to be able to split the path into a list of strings that are matched at each level. For C:\Windows\Sy , it needs to be split as “C:”, “Windows” and “Sy”. The default implementation of PySide.QtGui.QCompleter.splitPath() , splits the PySide.QtGui.QCompleter.completionPrefix() using QDir.separator() if the model is a PySide.QtGui.QFileSystemModel .

To provide completions, PySide.QtGui.QCompleter needs to know the path from an index. This is provided by PySide.QtGui.QCompleter.pathFromIndex() . The default implementation of PySide.QtGui.QCompleter.pathFromIndex() , returns the data for the edit role for list models and the absolute file path if the mode is a 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 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)

关于qt - 寻找具有分段完成/树模型的 QCompleter 示例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24687620/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com