0

I am trying to convert from pyqt5 to pyqt6 the code in the first answer of this post but so far no luck.

I managed to have something running with the following code:

#!/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())


However I encounter a strange bug when I start to type something in the ExtendedComboBox(for instance the letter h) and then I click on one of the filtered entries (for example Hello word) I have the following error:

 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)

The same does not happen in the original version therefore I think that there is something similar in pyqt6 which I cannot figure out

Woland
  • 1
  • 1
  • I never used PyQt6 before, so that's why I'm writting this as a comment than an answer. On this page from [PySide2 (which is PyQt5)](https://doc.qt.io/qtforpython-5/PySide2/QtWidgets/QComboBox.html#signals) the `activated` signal accepts two parameter types: *int* and *str*. But on the [PySide6 (PyQt6)](https://doc.qt.io/qtforpython/PySide6/QtWidgets/QComboBox.html#signals) documentation, the `activated` signal accepts only one type now: *int*. Try setting this line `self.activated[str].emit(self.itemText(index))` to `self.activated[int].emit(index)`. I hope it works or gives you some insight. – Carl HR Nov 23 '22 at 19:21
  • Signal overloads have been gradually removed in the last years, you should only keep using them when still actually required. In any case, check the official C++ API documentation whenever in doubt: the `activated` overloads have been declared obsolete in Qt5 since 5.14. If you need the string signal version, use [`textActivated()`](https://doc.qt.io/qt-6/qcombobox.html#textActivated). – musicamante Nov 23 '22 at 19:39
  • Both of the above comments work, thanks! You gave me precious insights and advices – Woland Nov 23 '22 at 21:24

1 Answers1

0

try this:

class ExtendedComboBox(QComboBox):
    #lineEdit = LineEdit()
    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)

    def focusInEvent(self, e):
        super().lineEdit().focusInEvent(e)
        QTimer.singleShot(0, self.lineEdit().selectAll)
        # QTimer.singleShot(0, self.showPopup)

    # 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)
ROOMBON
  • 1
  • 1