- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
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
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/
我有一个 QLineEdit,它有一个 QCompleter 对象。如果用户至少输入了一个字符,则会显示 QCompleter 的弹出菜单,但是当用户删除最后一个字符(从而使该字段留空)时,弹出菜单就
我正在使用 Qt4.6,并且我有一个带有 QCompleter 的 QComboBox。 通常的功能是根据前缀提供完成提示(这些提示可以在下拉列表中而不是内联 - 这是我的用法)。例如,给定 chic
我试图让 QCompleter 匹配几个用逗号分隔的等效选项。似乎没有简单的方法可以做到这一点,但有一行 QCompleter 引用引起了我的注意,它描述了函数 QCompleter::splitPa
我有表窗口小部件,其中一列需要具有文本自动填充功能。 每次用户请求新行时,我都会为该列运行以下代码: else if (i == COLUMN_DESCRIPICION){ Q
QCompleter 在大型数据集(大型模型)上运行速度稍慢:当我开始在 QCombobox 中输入字符时,它会花费几秒钟的时间来显示带有变体的自动完成弹出窗口,当输入第二个字符时,QComplete
我使用的是 Qt4.6,并且我有一个 QComboBox,里面有一个 QCompleter。 通常的功能是根据前缀提供完成提示(这些提示可以在下拉列表中而不是内联中 - 这是我的用法)。例如,给定 c
它没有显示任何弹出窗口或错误。它静静地什么都不做。 QStringList dictionary; dictionary inRawText); completer->setModel(new QSt
我对 Qt 很陌生。我试图在这里找到答案,但到目前为止没有成功。我在 main.cpp 文件中有一个复杂结构的 vector ,我想将它用作我在 mainwindow.cpp 的 void 函数中定义
我想做一个代码完成器,所以我将 QCompleter 子类化: http://hastebin.com/qeyumevisa.cpp 但是,当我尝试运行这段代码时,出现运行时错误: 调试输出显示: A
需要一些帮助。我有一个 QCompleter 和一些 QStringList,例如: QstringList list; list #include #include #include #in
我正在使用 Qt4.6,并且我有一个带有 QCompleter 的 QComboBox。 通常的功能是根据前缀提供完成提示(这些提示可以在下拉列表中而不是内联 - 这是我的用法)。例如,给定 chic
我有一个表,其中包含一些使用非常大的数字作为主键的键控记录。我有类似于下面的代码,它使用 QCompleter 自动完成此表中的查找。它有效,但显示的补全是使用科学记数法 (1234567 => 1.
有人问过关于覆盖 QCompleter 弹出位置的类似问题,但我仍然找不到有效的解决方案。我只是想将弹出窗口向下移动 5px 左右(我有一些特定的样式要求) 我尝试将 QListView 子类化并使用
我刚刚开始使用 PyQt5,这里有一个小文件: class Window(QWidget): def __init__(self): QWidget.__init__(self
我创建了一个自定义 QCompleter 类,它在弹出窗口中显示所有项目,其中包含 QLineEdit 的键入词。 现在所有项目都按字母顺序排列,如您在此处所见: 如果我输入“dab”然后按字母顺序输
我正在对 QCompleter 进行子类化,以赋予它一些特殊功能。我希望在模型中只有一个具有给定前缀的完成时触发 activated(),但这不是我遇到问题的地方。 我在我的子类中创建了一个虚拟 se
我已经用 QStringList 初始化了 QCompleter。而这个字符串列表有超过 30,000 个条目。我已在我的用户界面中连接到 Qlineedit。那里没有问题。问题在于,每当我在该 ql
在网上搜索了很多都没有找到正确的答案之后,我向你们寻求帮助...所以让我们看看我是否可以解释一下。 我正在使用 Qt 5 开发 UI,我想要一个具有自动完成功能的 lineEdit。我知道我可以使用
我都是,我只是效仿这个很好的例子: http://qt-project.org/doc/qt-4.8/tools-customcompleter.html 除了使用 modelFromFile 方法进
我想研究如何制作一个小的用户界面,用户可以在其中键入一些字母并根据给定的数据源(此处列出)获得一些建议,从而使搜索更容易。为此,我使用 Qt 的 QCompleter类。 在匹配元素中,键入的字母应使
我是一名优秀的程序员,十分优秀!