簡體   English   中英

PyQt5 QIcon 的“Ghost”出現在 QComboBox 的 QLineEdit 中

[英]PyQt5 “Ghost” of QIcon appears in QLineEdit of a QComboBox

我有一個QComboBox ,我希望每個項目都有自己的 QIcon,但 QIcon 應該只在下拉列表中可見。 我從上一個問題中得到了答案,它工作得很好:

在此處輸入圖像描述

如您所見,圖標僅出現在下拉列表中。 當我將QComboBox設置為可編輯時會出現問題,並且會發生這種情況:

在此處輸入圖像描述

這里 QIcon 的“幽靈”仍然存在,這反過來又取代了文本。 我的問題是:是什么原因造成的,我怎樣才能刪除這個“幽靈”,使文本正常顯示?

我的代碼:

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 還使用該圖標來設置 QLineEdit 的 position,當 QComboBox 可編輯時,您會看到該位移,如果您不想觀察,則必須重新計算幾何。 以下代碼通過 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)
        # ...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM