0

Since I updated python from 3.8 to 3.10 (with Linux Ubuntu 22.04), clear button in QlineEdit widget has become an ugly red cross. It was before a nice dark kind of rectangular button with a small cross inside. I wish I could switch back to the previous clear button without having to create a custom button, because the red cross is kind of disturbing as it seems to indicate an error in what you write in the QLineEdit widget.

Is there a way to do that in Qt Designer or programmatically?

musicamante
  • 41,230
  • 6
  • 33
  • 58

1 Answers1

0

It seems a bit unlikely that just updating Python would affect the icon.

The update probably involved other packages along with it (or they need rebuilding, they were uninstalled due to incompatibilities, etc), so I'd suggest to check that first.

In any case, you can set the icon using a specific stylesheet you could set for the top level window or even the application, so that it will be used for any QLineEdit with the clear button enabled:

QLineEdit > QToolButton {
    qproperty-icon: url(/path/to/icon.png);
}

Note that this will override all icons of QLineEdit, including those used for custom actions, so in that case you must explicitly set the object name of the button and use the proper selector in the QSS:

# this assumes that the clearButtonEnabled property is already set,
# otherwise it will crash

lineEdit.findChild(QToolButton).setObjectName('clearButton')
lineEdit.setStyleSheet('''
    QLineEdit > QToolButton#clearButton {
        qproperty-icon: url(/path/to/icon.png);
    }
''')

Also, see this related answer for other alternatives.

musicamante
  • 41,230
  • 6
  • 33
  • 58
  • Well I have tried those solutions without success, the related answer too. It seems there is an icon but it doesn't show, even after trying many size. Maybe I have missed something. – cyrille_saf Dec 05 '22 at 16:00
  • Do you mean that it still shows the red cross icon you mentioned in the question? That seems strange, as setting the icon should override anyway and eventually show a blank one if it doesn't find it. What Qt version are you using? – musicamante Dec 05 '22 at 18:24
  • My bad, it seems it needs a full path to icon, not a relative one. It is corrected and the icon was correctly displayed. Thanks for your help – cyrille_saf Dec 06 '22 at 13:21
  • @cyrille_saf Relative paths are also possible, *as long* as they're relative to the working dir (run the python interpreter in the same path of the resource). For any other case, you can always use Qt resources or dynamically create the path with `os.path` or the pathlib functions. – musicamante Dec 06 '22 at 13:47