简体   繁体   English

QToolButton-图标顶部的文本

[英]QToolButton - Text on top of icon

been searching the forums quite a bit..but couldnt find something reasonable. 一直在搜索论坛..但找不到合理的东西。 I have a QToolButton and an icon. 我有一个QToolButton和一个图标。 I dont want to bake the text into the icon image, I d rather have a text using btn.setText("Text"). 我不想将文本烘焙到图标图像中,而是使用btn.setText(“ Text”)获得文本。 The problem the text is set right underneath the icon which I dont want in my case. 问题是我的情况下我不需要的文本设置在图标的正下方。 It should be on top of the icon. 它应该在图标顶部。 Is there any way to do that? 有什么办法吗? I guess with a custom QToolButton..could someone help me out there? 我猜想有一个自定义的QToolButton ..有人可以帮我吗?

btn = QtGui.QToolButton()
btn.setText("Text")
icon = QtGui.QIcon()
icon.addPixmap(os.path.join('path_to_icon.png', QtGui.QIcon.Normal, QtGui.QIcon.On)
btn.setIcon(icon)

ideally: 理想情况下:

icon.setToolButtonStyle(QtCore.Qt.ToolButtonTextOnTopIcon)

Is there a way to offset the text using style sheet? 有没有一种方法可以使用样式表来补偿文本? Any help is appreciated 任何帮助表示赞赏

Found a good working solution! 找到了一个好的工作解决方案!

b = QtGui.QToolButton()
l = QtGui.QVBoxLayout(b)

b.setFixedSize(50, 50)
b.setIconSize(QtCore.QSize(50,50))
standardFont = QtGui.QFont("Arial", 10, QtGui.QFont.Bold)
standardFont.setCapitalization(QtGui.QFont.AllUppercase)

text = QtGui.QLabel("Text")

b.setIcon(QtGui.QIcon(os.path.join(ICON_DIR, 'csLighting_ftrackTime.png')))
l.setContentsMargins(0,0,0,0)
l.addStretch()
l.addWidget(text, 0, QtCore.Qt.AlignCenter)
text.setFont(standardFont)
b.show()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM