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 

def highlighted(self, i):
    print("Highlighted: ", 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


From: Serge K. <ska...@gmail.com>
Sent: 10 April 2018 16:11:19
To: Cristián Maureira-Fredes
Cc: pyside@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-fre...@qt.io<mailto:cristian.maureira-fre...@qt.io>> wrote:
Hello Serge,

After executing your script, I get the following output:
I do get the output from `selectionchange` and `highlighted`,
is this not the desire behavior?


From: PySide 
 on behalf of Serge K. <ska...@gmail.com<mailto:ska...@gmail.com>>
Sent: 09 April 2018 22:46:32
To: pyside@qt-project.org<mailto:pyside@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)
        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)

    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)

        return True

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

        for j in range(row, row + count)[::-1]:

        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.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.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("Current index", i, "selection changed ", self.cb.currentText())

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

if __name__ == '__main__':

PySide mailing list

Reply via email to