Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
144 views
in Technique[技术] by (71.8m points)

python - PyQt5 "Ghost" of QIcon appears in QLineEdit of a QComboBox

I have a QComboBox and I want each item to have its own QIcon, but the QIcon should only be visible in the dropdown. I took an answer from a previous question and it works pretty well:

enter image description here

As you can see, the Icon only appears in the dropdown. The problem arises when I set the QComboBox to be editable and this happens:

enter image description here

Here a "ghost" of the QIcon is still there which in turn displaces the text. My question is: What causes this and how can I remove this "ghost" so that the text appears normally?

My code:

from PyQt5.QtWidgets import (
    QApplication,
    QComboBox,
    QHBoxLayout,
    QStyle,
    QStyleOptionComboBox,
    QStylePainter,
    QWidget,
)
from PyQt5.QtGui import QIcon, QPalette
from PyQt5.QtCore import QSize


class EditCombo(QComboBox):
    def __init__(self, parent=None):
        super(EditCombo, self).__init__(parent)
        self.editable_index = 99
        self.currentIndexChanged.connect(self.set_editable)

    def setEditableAfterIndex(self, index):
        self.editable_index = index

    def set_editable(self, index: int) -> None:
        if index >= self.editable_index:
            self.setEditable(True)
        else:
            self.setEditable(False)
        self.update()

    def paintEvent(self, event):
        painter = QStylePainter(self)
        painter.setPen(self.palette().color(QPalette.Text))

        opt = QStyleOptionComboBox()
        self.initStyleOption(opt)
        opt.currentIcon = QIcon()
        opt.iconSize = QSize()

        painter.drawComplexControl(QStyle.CC_ComboBox, opt)
        painter.drawControl(QStyle.CE_ComboBoxLabel, opt)


class Example(QWidget):
    def __init__(self):
        super().__init__()

        self.initUI()

    def initUI(self):
        hbox = QHBoxLayout()

        edit_ico = QIcon("edit.png")
        empty_ico = QIcon("empty.png")  # For margin

        combo = EditCombo(self)
        combo.setEditableAfterIndex(2)
        combo.addItem(empty_ico, "Foo 1")
        combo.addItem(edit_ico, "Foo 2")
        combo.addItem(edit_ico, "Bar 1")
        combo.addItem(edit_ico, "Bar 2")

        hbox.addWidget(combo)

        self.setLayout(hbox)

        self.show()


def main():
    import sys

    app = QApplication(sys.argv)
    ex = Example()
    ex.setFixedWidth(300)
    sys.exit(app.exec_())


if __name__ == "__main__":
    main()
question from:https://stackoverflow.com/questions/65860967/pyqt5-ghost-of-qicon-appears-in-qlineedit-of-a-qcombobox

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

QComboBox also uses the icon to set the position of the QLineEdit that is used when the QComboBox is editable so you see that displacement, if you don't want to observe that then you have to recalculate the geometry. The following code does it through a QProxyStyle:

class ProxyStyle(QProxyStyle):
    def subControlRect(self, control, option, subControl, widget=None):
        r = super().subControlRect(control, option, subControl, widget)
        if control == QStyle.CC_ComboBox and subControl == QStyle.SC_ComboBoxEditField:
            if widget.isEditable():
                widget.lineEdit().setGeometry(r)
        return r
class EditCombo(QComboBox):
    def __init__(self, parent=None):
        super(EditCombo, self).__init__(parent)
        self._style = ProxyStyle(self.style())
        self.setStyle(self._style)
        self.editable_index = 99
        self.currentIndexChanged.connect(self.set_editable)
        # ...

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...