2 回答

TA貢獻(xiàn)1906條經(jīng)驗(yàn) 獲得超10個(gè)贊
我使用的是 PySide2 5.6.0a1,因?yàn)檫@是 Anaconda 在 Python 2.7 環(huán)境中安裝的一個(gè)。@eyllanesc 指出這是一個(gè)早期和過時(shí)的版本,可能有問題。
當(dāng)我在 Python 3.7 環(huán)境中使用 PySide2-5.13.1 嘗試相同的代碼時(shí),一切都按預(yù)期工作。

TA貢獻(xiàn)1895條經(jīng)驗(yàn) 獲得超7個(gè)贊
我沒有 PySide2,但在大多數(shù)情況下,我認(rèn)為您所要做的就是用 PySide2 替換我的 PyQt5 引用——因?yàn)檫@就是我所做的一切讓你的程序從 PySide2 切換到 PyQt5 ——還有一點(diǎn)重組和微調(diào)給了我以下功能代碼:
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_())
我認(rèn)為問題是您試圖將 Signals/Slots 與非 QObject 函數(shù)(又名)一起使用,您的 change_option 函數(shù)與從 QObject 繼承的任何內(nèi)容都沒有直接關(guān)聯(lián),所以我不確定它在做什么或沒有在做什么,但是只是一個(gè)猜測,因?yàn)槲宜龅囊磺卸急环湃肓艘粋€(gè)正常的 Qt 結(jié)構(gòu)中,它工作得很好
添加回答
舉報(bào)