简体   繁体   中英

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:

在此处输入图像描述

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:

在此处输入图像描述

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()

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.editable_index = 99
        self.currentIndexChanged.connect(self.set_editable)
        # ...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM