You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PySide开发:如何将按钮文本置于图标前方而非右侧?

解决PySide QPushButton文本与图标顺序问题

要让按钮文本显示在图标前方(左侧),替代默认的图标左、文本右布局,只需在按钮初始化代码中添加一行设置即可,以下是两种可行方案:

方案1:使用setToolButtonStyle方法

在设置图标和样式表之后,添加枚举配置来指定文本与图标的顺序:

from PySide6.QtCore import Qt  # PySide2对应导入PySide2.QtCore.Qt
cBut.setToolButtonStyle(Qt.ToolButtonTextBeforeIcon)

方案2:通过样式表控制图标位置

修改按钮的StyleSheet,添加icon-position: right;属性,让图标靠右显示,文本自然会处于左侧:

cBut.setStyleSheet(f"font-family: {self.language}; font-size: 12px; font-weight: bold; background-color: transparent; border: none; icon-position: right;")

修改后的完整循环代码(以方案1为例)

from PySide6.QtCore import Qt

for i, buttonText in enumerate(self.buttonTextLeft):
    cBut=QPushButton(buttonText, self)
    cBut.setIcon(QIcon(resPixmap))
    cBut.setIconSize(resPixmap.size())
    # 设置文本在前、图标在后
    cBut.setToolButtonStyle(Qt.ToolButtonTextBeforeIcon)
    cBut.setStyleSheet(f"font-family: {self.language}; font-size: 12px; font-weight: bold; background-color: transparent; border: none;")
    cBut.setGeometry(0, int(self.bTL*self.height()),int(self.topButtonWidth*self.width()),int(self.leftButtonWidth*self.height()))
    cBut.clicked.connect(lambda checked,index=i:self.optionButtonsClickLeft(index))
    self.optionButtonsLeft.append(cBut)
    self.bTL+=self.leftButtonWidth

内容的提问来源于stack exchange,提问作者Ben Broughton

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.24 03:40:04