2 回答
TA贡献1906条经验 获得超10个赞
我使用的是 PySide2 5.6.0a1,因为这是 Anaconda 在 Python 2.7 环境中安装的一个。@eyllanesc 指出这是一个早期和过时的版本,可能有问题。
当我在 Python 3.7 环境中使用 PySide2-5.13.1 尝试相同的代码时,一切都按预期工作。
TA贡献1895条经验 获得超7个赞
我没有 PySide2,但在大多数情况下,我认为您所要做的就是用 PySide2 替换我的 PyQt5 引用——因为这就是我所做的一切让你的程序从 PySide2 切换到 PyQt5 ——还有一点重组和微调给了我以下功能代码:
from sys import exit as sysExit
from PyQt5.QtCore import Qt, QSortFilterProxyModel, QStringListModel, pyqtSlot
from PyQt5.QtWidgets import QApplication, QWidget, QCompleter, QComboBox, QCompleter, QHBoxLayout
class ExtendedComboBox(QComboBox):
def __init__(self):
QComboBox.__init__(self)
self.setFocusPolicy(Qt.StrongFocus)
self.setEditable(True)
# add a filter model to filter matching items
self.pFilterModel = QSortFilterProxyModel(self)
self.pFilterModel.setFilterCaseSensitivity(Qt.CaseInsensitive)
self.pFilterModel.setSourceModel(self.model())
# add a completer, which uses the filter model
self.completer = QCompleter(self)
self.completer.setModel(self.pFilterModel)
# always show all (filtered) completions
self.completer.setCompletionMode(QCompleter.UnfilteredPopupCompletion)
self.setCompleter(self.completer)
# connect signals
self.lineEdit().textEdited.connect(self.pFilterModel.setFilterFixedString)
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):
if text:
index = self.findText(text)
self.setCurrentIndex(index)
# self.activated.emit(self.itemText(index))
# on model change, update the models of the filter and completer as well
def setModel(self, model):
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)
self.setModelColumn(column)
class MainApp(QWidget):
def __init__(self):
QWidget.__init__(self)
string_list = ['hola muchachos', 'adios amigos', 'hello world', 'good bye']
self.combo = ExtendedComboBox()
# either fill the standard model of the combobox
self.combo.addItems(string_list)
self.combo.currentIndexChanged[str].connect(self.change_option)
# or use another model
#combo.setModel(QStringListModel(string_list))
self.resize(300, 100)
self.combo.resize(300, 50)
HBox = QHBoxLayout()
HBox.addWidget(self.combo)
self.setLayout(HBox)
@pyqtSlot(str)
def change_option(self, text):
print(text)
if __name__ == "__main__":
MainThred = QApplication([])
MainGui = MainApp()
MainGui.show()
sysExit(MainThred.exec_())
我认为问题是您试图将 Signals/Slots 与非 QObject 函数(又名)一起使用,您的 change_option 函数与从 QObject 继承的任何内容都没有直接关联,所以我不确定它在做什么或没有在做什么,但是只是一个猜测,因为我所做的一切都被放入了一个正常的 Qt 结构中,它工作得很好
添加回答
举报