gpt4 book ai didi

python - 具有自动补全功能的 QComboBox 在 PyQt4 中有效,但在 PySide 中无效

转载 作者:行者123 更新时间:2023-12-01 05:09:37 25 4
gpt4 key购买 nike

我有一个带有自定义完成器的组合框,它在 PyQt4 中运行良好,但在 PySide 中不起作用。

我已经验证新的完成器正在替换 QComboBox 的内置完成器,因为内联完成不再发生。但是,当使用 PySide 运行时,完成程序不会弹出已过滤的选项列表。

我还尝试确保所有文本都是 str 或全部 unicode 以避免 PyQt API 1 与 QStrings 和 PySide 使用 Python unicode 类型之间的差异。更改文本类型对 PyQt 或 PySide 的行为没有影响(PyQt 继续工作,PySide 不起作用)。

这是我的代码:

from PySide import QtCore
from PySide import QtGui

#from PyQt4 import QtCore
#from PyQt4 import QtGui


class AdvComboBox(QtGui.QComboBox):
def __init__(self, parent=None):
super(AdvComboBox, self).__init__(parent)

self.setFocusPolicy(QtCore.Qt.StrongFocus)
self.setEditable(True)

# add a filter model to filter matching items
self.pFilterModel = QtGui.QSortFilterProxyModel(self)
self.pFilterModel.setFilterCaseSensitivity(QtCore.Qt.CaseInsensitive)
self.pFilterModel.setSourceModel(self.model())

# add a completer, which uses the filter model
self.completer = QtGui.QCompleter(self.pFilterModel, self)
# always show all (filtered) completions
self.completer.setCompletionMode(QtGui.QCompleter.UnfilteredPopupCompletion)

self.setCompleter(self.completer)

# connect signals

def filter(text):
print "Edited: ", text, "type: ", type(text)
self.pFilterModel.setFilterFixedString(str(text))

self.lineEdit().textEdited[unicode].connect(filter)
self.completer.activated.connect(self.on_completer_activated)

# on selection of an item from the completer, select the corresponding item from combobox
def on_completer_activated(self, text):
print "activated"
if text:
print "text: ", text
index = self.findText(str(text))
print "index: ", index
self.setCurrentIndex(index)


# on model change, update the models of the filter and completer as well
def setModel(self, model):
super(AdvComboBox, self).setModel(model)
self.pFilterModel.setSourceModel(model)
self.completer.setModel(self.pFilterModel)


# on model column change, update the model column of the filter and completer as well
def setModelColumn(self, column):
self.completer.setCompletionColumn(column)
self.pFilterModel.setFilterKeyColumn(column)
super(AdvComboBox, self).setModelColumn(column)


if __name__ == "__main__":
import sys

app = QtGui.QApplication(sys.argv)

combo = AdvComboBox()

names = ['bob', 'fred', 'bobby', 'frederick', 'charles', 'charlie', 'rob']

# fill the standard model of the combobox
combo.addItems(names)
combo.setModelColumn(0)
combo.resize(300, 40)
combo.show()

sys.exit(app.exec_())

最佳答案

我在写问题时想通了......

看来,虽然 PySide QCompleter documentation列出了使用模型和父级初始化 QCompleter 的选项,但它实际上不起作用。

解决方案是在完成器初始化后设置其模型。

这是工作代码:

from PySide import QtCore
from PySide import QtGui


class AdvComboBox(QtGui.QComboBox):
def __init__(self, parent=None):
super(AdvComboBox, self).__init__(parent)

self.setFocusPolicy(QtCore.Qt.StrongFocus)
self.setEditable(True)

# add a filter model to filter matching items
self.pFilterModel = QtGui.QSortFilterProxyModel(self)
self.pFilterModel.setFilterCaseSensitivity(QtCore.Qt.CaseInsensitive)
self.pFilterModel.setSourceModel(self.model())

# add a completer
self.completer = QtGui.QCompleter(self)
#Set the model that the QCompleter uses
# - in PySide doing this as a separate step worked better
self.completer.setModel(self.pFilterModel)
# always show all (filtered) completions
self.completer.setCompletionMode(QtGui.QCompleter.UnfilteredPopupCompletion)

self.setCompleter(self.completer)

# connect signals

def filter(text):
print "Edited: ", text, "type: ", type(text)
self.pFilterModel.setFilterFixedString(str(text))

self.lineEdit().textEdited[unicode].connect(filter)
self.completer.activated.connect(self.on_completer_activated)

# on selection of an item from the completer, select the corresponding item from combobox
def on_completer_activated(self, text):
print "activated"
if text:
print "text: ", text
index = self.findText(str(text))
print "index: ", index
self.setCurrentIndex(index)


# on model change, update the models of the filter and completer as well
def setModel(self, model):
super(AdvComboBox, self).setModel(model)
self.pFilterModel.setSourceModel(model)
self.completer.setModel(self.pFilterModel)


# on model column change, update the model column of the filter and completer as well
def setModelColumn(self, column):
self.completer.setCompletionColumn(column)
self.pFilterModel.setFilterKeyColumn(column)
super(AdvComboBox, self).setModelColumn(column)


if __name__ == "__main__":
import sys

app = QtGui.QApplication(sys.argv)

combo = AdvComboBox()

names = ['bob', 'fred', 'bobby', 'frederick', 'charles', 'charlie', 'rob']

# fill the standard model of the combobox
combo.addItems(names)
combo.setModelColumn(0)
combo.resize(300, 40)
combo.show()

sys.exit(app.exec_())

关于python - 具有自动补全功能的 QComboBox 在 PyQt4 中有效,但在 PySide 中无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24456460/

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