我正试图将此post的第一个答案中的代码从pyqt5转换为pyqt6,但到目前为止没有运气。
我设法用下面的代码运行了一些东西:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from PyQt6.QtWidgets import QApplication
from PyQt6.QtCore import QStringListModel
from PyQt6 import QtCore, QtGui, QtWidgets
from PyQt6.QtCore import Qt, QSortFilterProxyModel
from PyQt6.QtWidgets import QCompleter, QComboBox
import sys
class ExtendedComboBox(QComboBox):
def __init__(self, parent=None):
super(ExtendedComboBox, self).__init__(parent)
self.setFocusPolicy(Qt.FocusPolicy.StrongFocus)
self.setEditable(True)
# add a filter model to filter matching items
self.pFilterModel = QSortFilterProxyModel(self)
self.pFilterModel.setFilterCaseSensitivity(Qt.CaseSensitivity.CaseInsensitive)
self.pFilterModel.setSourceModel(self.model())
# add a completer, which uses the filter model
self.completer = QCompleter(self.pFilterModel, self)
# always show all (filtered) completions
self.completer.setCompletionMode(QCompleter.CompletionMode.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[str].emit(self.itemText(index))
# on model change, update the models of the filter and completer as well
def setModel(self, model):
super(ExtendedComboBox, 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(ExtendedComboBox, self).setModelColumn(column)
if __name__ == "__main__":
app = QApplication(sys.argv)
string_list = ['hola muchachos', 'adios amigos', 'hello world', 'good bye']
combo = ExtendedComboBox()
# either fill the standard model of the combobox
combo.addItems(string_list)
# or use another model
#combo.setModel(QStringListModel(string_list))
combo.resize(300, 40)
combo.show()
sys.exit(app.exec())
然而,当我开始在ExtendedComboBox
中键入一些内容(例如字母h
),然后单击其中一个过滤后的条目(例如Hello word
)时,我遇到了一个奇怪的错误,我有以下错误:
python test.py
Traceback (most recent call last):
File "test.py", line 38, in on_completer_activated
self.activated[str].emit(self.itemText(index))
KeyError: 'there is no matching overloaded signal'
Aborted (core dumped)
同样的情况在原始版本中没有发生,因此我认为在pyqt6中有一些类似的东西,我无法弄清楚
1条答案
按热度按时间wgx48brx1#
试试这个: