将图标移动到QCheckBox中文本的右侧。

问题描述 投票:2回答:1

我想把一个QCheckBox的图标从标签的左边移到右边。

我已经检查了这些帖子。

但这两种方法似乎都不适用于QCheckBox。事实上,第二种解决方案是我目前最好的方案,但我希望图标只是在标签的右边,而不是一直对齐到小组件的右边。

这是我的实验。

from PyQt5 import QtGui
from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import QWidget, QVBoxLayout, QCheckBox, QStyle, QApplication, QLabel, QHBoxLayout


class CheckBoxWIcon(QCheckBox):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        icon = self.icon()
        icon_size = self.iconSize()
        # remove icon
        self.setIcon(QtGui.QIcon())
        label_icon = QLabel()
        label_icon.setAttribute(Qt.WA_TranslucentBackground)
        label_icon.setAttribute(Qt.WA_TransparentForMouseEvents)
        lay = QHBoxLayout(self)
        lay.setContentsMargins(0, 0, 0, 0)
        lay.addWidget(label_icon, alignment=Qt.AlignRight)
        label_icon.setPixmap(icon.pixmap(icon_size))


app = QApplication([])
mw = QWidget()
layout = QVBoxLayout()

test1 = QCheckBox("Default")
test1.setIcon(app.style().standardIcon(QStyle.SP_MediaSkipForward))
test1.setStyleSheet("""QCheckBox { text-align: right; }""")

test2 = QCheckBox("Using style-sheet")
test2.setIcon(app.style().standardIcon(QStyle.SP_MediaSkipForward))
test2.setStyleSheet("""QCheckBox { text-align: left; }""")

test3 = QCheckBox("Using layout direction")
test3.setIcon(app.style().standardIcon(QStyle.SP_MediaSkipForward))
test3.setLayoutDirection(Qt.RightToLeft)

test4 = CheckBoxWIcon("Custom class", icon=QApplication.style().standardIcon(QStyle.SP_MediaSkipForward))

layout.addWidget(test1)
layout.addWidget(test2)
layout.addWidget(test3)
layout.addWidget(test4)
mw.setLayout(layout)

mw.show()
app.exec()

enter image description here

希望的输出:

enter image description here

python pyqt pyqt5 qcheckbox
1个回答
3
投票

一个可能的解决方案是使用QProxyStyle来修改绘画。

from PyQt5.QtCore import QRect, Qt
from PyQt5.QtGui import QIcon
from PyQt5.QtWidgets import QApplication, QCheckBox, QProxyStyle, QStyle, QWidget


class IconProxyStyle(QProxyStyle):
    def drawControl(self, element, option, painter, widget=None):
        if element == QStyle.CE_CheckBoxLabel:
            offset = 4
            icon = QIcon(option.icon)
            option.icon = QIcon()

            super().drawControl(element, option, painter, widget)

            alignment = self.visualAlignment(
                option.direction, Qt.AlignLeft | Qt.AlignVCenter
            )
            if not self.proxy().styleHint(QStyle.SH_UnderlineShortcut, option, widget):
                alignment |= Qt.TextHideMnemonic
            r = painter.boundingRect(
                option.rect, alignment | Qt.TextShowMnemonic, option.text
            )

            option.rect.setLeft(r.right() + offset)
            option.text = ""
            option.icon = icon

        super().drawControl(element, option, painter, widget)


def main():
    import sys

    app = QApplication(sys.argv)
    app.setStyle("fusion")
    app.setStyle(IconProxyStyle(app.style()))
    button = QCheckBox(
        "Test\nwith\nQProxyStyle",
        icon=QApplication.style().standardIcon(QStyle.SP_MediaSkipForward),
    )
    # button.setStyle(IconProxyStyle(button.style()))

    button.show()

    sys.exit(app.exec_())


if __name__ == "__main__":
    main()
© www.soinside.com 2019 - 2024. All rights reserved.