Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

PyQt: QCombobox background text color not reflected in selected state



  • Hi guys

    The following code works but doesn't do in full what exactly is needed:

    @nameList = ('John','Tom','Henry','Michelle','Ashish','Jo-Huang', ...)
    colorNameList = ('Michelle','Jennifer','Claudia','JimSung', ...)
    callBackObj.NameComboBox.clear()
    callBackObj.NameComboBox.addItem(QString('Account Names'))
    model = callBackObj.NameComboBox.model()
    for name in nameList:
    item = PyQt4.QtGui.QStandardItem(str(account))
    if name in colorNameList:
    item.setBackground(PyQt4.QtGui.QColor('red'))
    model.appedRow(item)@

    Now whenever I expand and view the dropdown, the names in colorNameList appear in red background. So far so good. However when I select one of these red-background names they do not show as red-background when left selected.

    Any ideas how I can ensure that on selection red items appear red and non-red appears non-red.

    Many thanks!

    Rahul


  • Lifetime Qt Champion

    Hi and welcome to devnet,

    Just to be sure I understand you correctly: you would like the selection highlight to be in a color similar to the background color ?



  • Hi SGaist

    Thanks for your message and welcome.

    Yes, so I want selection highlight background to have the same background color as it had when it was in not selected state.

    To elaborate consider the following example that comboBox has following 3 entries:

    <NoBackgroundColor>John</NoBackgroundColor>
    <NoBackgroundColor>Henry</NoBackgroundColor>
    <REDBackgroundColor>Michelle</REDBackgroundColor>

    Now when I select John OR Henry, the selected state with dropdown showing either of these values will have no background color; that is fine and expected. However on selecting Michelle the Red background is lost and it switches to No background color. I want it to retain its background Red color in selected state.

    Thanks again
    Rahul


  • Lifetime Qt Champion

    Then you should implement a custom QStyledItemDelegate and update the color of the rectangle with the state is selected


Log in to reply