将图标移动到 QCheckBox 中文本的右侧
Posted
技术标签:
【中文标题】将图标移动到 QCheckBox 中文本的右侧【英文标题】:Move icon to right side of text in a QCheckBox 【发布时间】:2020-06-03 12:24:17 【问题描述】:我正在尝试将 QCheckBox 的图标从标签左侧移到右侧。
我查看了这些帖子:
PyQt4 QPushButton text and icon alignment Align icon on the right and center the text in a QPushButton但似乎两者都不适用于 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()
期望的输出:
【问题讨论】:
【参考方案1】:一种可能的解决方案是使用 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()
【讨论】:
以上是关于将图标移动到 QCheckBox 中文本的右侧的主要内容,如果未能解决你的问题,请参考以下文章