qlabel怎么设置文字在图标右边
时间: 2024-04-09 09:28:43 浏览: 80
您可以使用`QHBoxLayout`布局来实现将文本置于图标右侧的效果。首先,创建一个`QLabel`和一个`QIcon`来分别表示文本和图标。然后,创建一个水平布局并将文本标签和图标标签添加到水平布局中。最后,将水平布局设置为窗口的布局。
以下是一个示例代码:
```python
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QLabel, QHBoxLayout
from PyQt5.QtGui import QIcon, QPixmap
class Example(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
# 创建文本标签和图标
label = QLabel('文字')
icon = QIcon('icon.png')
pixmap = icon.pixmap(16, 16) # 设置图标大小
# 创建水平布局
hbox = QHBoxLayout()
hbox.addWidget(label)
hbox.addStretch(1) # 添加一个伸缩项,将图标推到右侧
hbox.addWidget(QLabel().setPixmap(pixmap))
self.setLayout(hbox)
self.setGeometry(300, 300, 300, 200)
self.setWindowTitle('示例')
self.show()
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())
```
在上面的示例中,我们使用了一个伸缩项`addStretch(1)`来将图标推到右侧。您可以根据需要调整布局和图标大小。
阅读全文
相关推荐


















