Important: Please read the Qt Code of Conduct -

[Solved]How to change font color of QListWidgetItem when it is in edit mode

  • Hello there guys!
    Again, I am having hard time to do something in Qt. I have a QListWidget with the following StyleSheet for its items:
    QListView:item:selected { color: black; background-color: orange; }
    Using the above, the items in the list have a black font when I select one of them.
    What I want now is to have a different font color (white) when one of them is in edit mode.
    Is there anyway to do this with CSS? The only solution I can think of is using C++ code and is the following:
    Set the font color to (white) using a StyleSheet when I am entering the edit mode. (in my code I call QListWidget::editItem(). So I will set the new styleSheet before calling this). Then, I will use this connection to catch the closeEditor signal and reset the item color somehow using a styleSheet:
    connect( _stateListWidget->itemDelegate(), SIGNAL(closeEditor(QWidget*)), this, SLOT(editorWasClosed(QWidget*)) );

    I don't like this solution. There must be an easier way to do this. Any ideas? Thank you!


    Edit: the square-bracket version of the code tag does not work properly for the forum, use the @ tags instead please; Andre

  • What you might try, is use the fact that the delegate is a child widget of the viewport. That means, that you should be able to do something along these lines:

    QWidget QWidget { color: red; background-color: white }
    and set that on the viewport.

    I did not try this myself, but I think something like this should work.

  • Ohh my god... you rock :D It worked! :D I think I need to read some Qt tutorials or get a Qt book because I don't know a lot of basic stuff. I didn't know about viewport for example :P
    Thanks again my friend!


Log in to reply