[PySide] QCombobox : Signal not emitted

Cristián Maureira-Fredes Cristian.Maureira-Fredes at qt.io
Wed Apr 11 09:23:24 CEST 2018


Hello Serge,

I'm glad you solved your issue,
for more dynamical conversations I recommend you
to join our IRC channel in Freenode #qt-pyside
If you don't like IRC, you can join through Gitter
using the old/deprecated PySide repository,
and last but not least, we have a lonely keybase team
you can also join:

Here you can find the links: https://wiki.qt.io/PySide2#Community

Cheers!

________________________________________
From: Serge K. <skab12 at gmail.com>
Sent: 10 April 2018 17:57:53
To: Cristián Maureira-Fredes
Cc: pyside at qt-project.org
Subject: Re: [PySide] QCombobox : Signal not emitted

Ok, I found it!

Thank you really much Cristian! here the code (quite ugly :-)) for handling the click:


self.cb.view().installEventFilter(self)
self.cb.view().viewport().installEventFilter(self)

def eventFilter(self, widget, event):
    if event.type() == QEvent.MouseButtonRelease:  # and isinstance(widget, QComboBox):
        print("--eventFilter() mouse_clicked on " + str(widget.objectName()))
        print(widget.parent().parent().parent().view().currentIndex())
        current_idx = self.cb.view().currentIndex().row()
        self.cb.setCurrentIndex(current_idx)
        self.cb.activated.emit(current_idx)
        self.cb.hidePopup()
        return False
    return super(combodemo, self).eventFilter(widget, event)

Serge K.

On Tue, Apr 10, 2018 at 11:23 AM, Serge K. <skab12 at gmail.com<mailto:skab12 at gmail.com>> wrote:
Ok, I see. Thank you really much for this information.

Indeed, I want to add the clicking behavior for selecting an option. Highlighted was a quick test to see if it was working. I am surprised that keyboard events are implemented and not mouseEvents.

If I add an eventFilter to my QCombobox for catching MouseButtonRelease event, how can I get the clicked option? Or Do you have an easier way to do that Cristian?


self.cb.view().installEventFilter(self)
self.cb.view().viewport().installEventFilter(self)

def eventFilter(self, widget, event):
    if event.type() == QEvent.MouseButtonRelease:
        print("--eventFilter() mouse_clicked on " + str(widget.objectName()))
        my_idx =
        self.cb.setCurrentIndex(my_idx)
        return False
    return False



On Tue, Apr 10, 2018 at 10:33 AM, Cristián Maureira-Fredes <Cristian.Maureira-Fredes at qt.io<mailto:Cristian.Maureira-Fredes at qt.io>> wrote:
Hello Serge,

I was using my keyboard to navigate the QComboBox options,
because hovering the options with the mouse just call the
highlighted slot.

If you want to do something on highlighted, you can add for example the following:

def highlighted(self, i):
    print("Highlighted: ", i)
    self.cb.setCurrentIndex(i)

In that way, when you move the mouse over the options,
the  currentIndexChanged signal will be emitted.

I don't know if you want a special behavior on this actions,
but remember to play around with mouseEvents if you want
to add clicking behavior.

I'm Running Linux, Python 3.6.4 + PySide2 5.9

Cheers

________________________________________
From: Serge K. <skab12 at gmail.com<mailto:skab12 at gmail.com>>
Sent: 10 April 2018 16:11:19
To: Cristián Maureira-Fredes
Cc: pyside at qt-project.org<mailto:pyside at qt-project.org>
Subject: Re: [PySide] QCombobox : Signal not emitted

Thanks Cristian,

This is the desired behavior but I do not get this result... That is really weird... Can you tell me what is your environment?

Me : windows 10 + anaconda python 3.6.4 + pyside2==5.9.0a1


On Apr 10, 2018 3:09 AM, "Cristián Maureira-Fredes" <Cristian.Maureira-Fredes at qt.io<mailto:Cristian.Maureira-Fredes at qt.io><mailto:Cristian.Maureira-Fredes at qt.io<mailto:Cristian.Maureira-Fredes at qt.io>>> wrote:
Hello Serge,

After executing your script, I get the following output:
https://pastebin.com/2L3VYR0n
I do get the output from `selectionchange` and `highlighted`,
is this not the desire behavior?

Cheers

________________________________________
From: PySide <pyside-bounces+cristian.maureira-fredes=qt.io at qt-project.org<mailto:qt.io at qt-project.org><mailto:qt.io at qt-project.org<mailto:qt.io at qt-project.org>>> on behalf of Serge K. <skab12 at gmail.com<mailto:skab12 at gmail.com><mailto:skab12 at gmail.com<mailto:skab12 at gmail.com>>>
Sent: 09 April 2018 22:46:32
To: pyside at qt-project.org<mailto:pyside at qt-project.org><mailto:pyside at qt-project.org<mailto:pyside at qt-project.org>>
Subject: [PySide] QCombobox : Signal not emitted


Hi Everyone,

After setting a custom model on QCombobox, I do not receive anymore currentIndexChanged signal from my QCombobox (look below as an example) Did I do something wrong?


from PySide2.QtCore import *
from PySide2.QtGui import *
from PySide2.QtWidgets import *


class CustomModel(QAbstractListModel):
    """ Protocol model """

    def __init__(self, *args, **kwargs):
        super(CustomModel, self).__init__(*args, **kwargs)

        self._protocol_list = []

    def add_protocol(self, data, row=0):
        if not row:
            row = len(self._protocol_list)

        self.beginInsertRows(QModelIndex(), row, row)
        self._protocol_list.insert(row, data)
        self.endInsertRows()
        return self.index(len(self._protocol_list) - 1)

    def remove_protocol(self, item):
        for row, data in enumerate(self._protocol_list):
            if data.name.lower() == item.name.lower():
                self.beginRemoveRows(QModelIndex(), row, row)
                self._protocol_list.pop(row)
                self.endRemoveRows()

    def rowCount(self, parent=QModelIndex()):
        if parent.isValid():
            return 0
        return len(self._protocol_list)


    def insertRows(self, row, count, index=QModelIndex()):
        self.beginInsertRows(QModelIndex(), row, row + count - 1)

        for j in range(row, row + count):
            self._protocol_list.insert(j, None)

        self.endInsertRows()
        return True

    def removeRows(self, row, count, index=QModelIndex()):
        self.beginRemoveRows(QModelIndex(), row, row + count - 1)

        for j in range(row, row + count)[::-1]:
            self._protocol_list.pop(j)

        self.endRemoveRows()
        return True

    def headerData(self, section, orientation, role=Qt.EditRole):
        return None

    def data(self, index, role=Qt.DisplayRole):
        if not index.isValid():
            return None

        current_protocol = self._protocol_list[index.row()]

        if role == Qt.DisplayRole or role == Qt.EditRole:
            return current_protocol[0]

        return None

    def setData(self, index, value, role=Qt.EditRole):
        if role != Qt.EditRole:
            return True

        if index.isValid() and 0 <= index.row() < len(self._protocol_list):
            current_protocol = self._protocol_list[index.row()]
            if index.column() == 0:
                current_protocol[0] =  value

                self.dataChanged.emit(index, index)
                return True

        return False

    def flags(self, index):
        if index.isValid():
            return Qt.ItemFlags(Qt.ItemIsEnabled | Qt.ItemIsEditable)
        return QAbstractListModel.flags(index)


class combodemo(QWidget):
    def __init__(self, parent=None):
        super(combodemo, self).__init__(parent)

        layout = QHBoxLayout()
        self.cb = QComboBox()
        self.model = CustomModel()
        self.cb.setModel(self.model)
        self.cb.setModelColumn(0)
        self.model.add_protocol(("c", "A"))
        self.model.add_protocol(("c++", "B"))
        self.model.add_protocol(("Java", "C"))
        self.model.add_protocol(("C#", "D"))
        self.model.add_protocol(("Python", "E"))

        self.cb.currentIndexChanged.co<http://self.cb.currentIndexChanged.co>nnect(self.selectionchange)
        self.cb.highlighted.connect(self.highlighted)

        layout.addWidget(self.cb)
        self.setLayout(layout)
        self.setWindowTitle("combo box demo")

    def highlighted(self, i):
        print("Highlighted: ", i)

    def selectionchange(self, i):
        print("Items in the list are :")

        for count in range(self.cb.count()):
            print(self.cb.itemText(count))
        print("Current index", i, "selection changed ", self.cb.currentText())



def main():
    app = QApplication(sys.argv)
    ex = combodemo()
    ex.show()
    sys.exit(app.exec_())


if __name__ == '__main__':
    main()







More information about the PySide mailing list