python - 控制 QToolButton (PyQt5) 的大小/样式

标签 python pyqt pyqt5

我正在尝试使用 QToolButton (切换显示/隐藏)和 QTextEdit (显示信息)为对话框实现下拉“详细信息”显示。我正在向右箭头(隐藏)和向下箭头(显示)之间切换以指示显示器的当前状态。

    # Create a button to toggle the details frame.              
    self._detailsbutton = QToolButton()                                     
    self._detailsbutton.setCheckable(True)                                  
    self._detailsbutton.setChecked(False)                                   
    self._detailsbutton.setArrowType(Qt.RightArrow)                         
    self._detailsbutton.setToolButtonStyle(Qt.ToolButtonIconOnly)           
    self._detailsbutton.toggled.connect(self.showDetails)

默认情况下,QToolButton 相当大。

enter image description here

我一直在试图找到一种方法来缩小这个按钮的大小。我发现的唯一有希望的事情是 QWidget.setFixedSize 方法,但使用此方法会导致工具按钮箭头看起来偏离中心并被剪裁。

    # Create a button to toggle the details frame.              
    self._detailsbutton = QToolButton()                                     
    self._detailsbutton.setCheckable(True)                                  
    self._detailsbutton.setChecked(False)                                   
    self._detailsbutton.setArrowType(Qt.RightArrow)                         
    self._detailsbutton.setToolButtonStyle(Qt.ToolButtonIconOnly)           
    self._detailsbutton.setFixedSize( 10, 10 )                              
    self._detailsbutton.toggled.connect(self.showDetails)

enter image description here

我尝试将大小设置为不同的值 (5 - 15),但箭头无法正确居中。有谁知道如何在保持箭头居中的同时缩小工具按钮的大小?我也有兴趣了解如何控制显示箭头的大小。

一如既往,如果有人有更好的方法来做到这一点,我会洗耳恭听。

如果您能告诉我如何关闭按钮周围的边框,并仅在灰色背景下显示裸露箭头,那就加分了。

最佳答案

在 Linux 和 Windows 10 上,工具按钮和箭头显示正确,因此我无法重现您的问题。但正如您的 png 所示,只有工具按钮的大小发生了变化,箭头似乎具有旧的大小。

如所写in documentation工具按钮的大小可以通过setIconSize来调整。所以你可以尝试一下,这在linux上可以工作

self._detailsbutton.setIconSize(QtCore.QSize(int, int))

如果这不起作用,请尝试根据工具按钮大小设置 iconSize

bw = 10                                 # buttonWidth
iw = int(bw*.0.8)                       # iconWidth
self._detailsbutton.setFixedSize( bw, bw )
self._detailsbutton.setIconSize(QtCore.QSize(iw,iw))

关闭按钮周围的边框

self._detailsbutton.setStyleSheet('border: none;')

关于python - 控制 QToolButton (PyQt5) 的大小/样式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34387293/

相关文章:

python - 多次运行和退出 PyQt 应用程序

python - 导入错误 : No module named 'tweepy.streaming' ; 'tweepy' is not a package

python - call() 中忽略的参数

python - 用列表填充 QComboBox

python - 从 QThread 生成并插入到 QTreeWidget 中的 QWidget 的正确父子关系

python - 无法启动设计的pyQt应用程序

python - 在 Django 中对紧耦合模型进行单元测试

python - 从子外部调用父方法

python - 如何调整 PyQt 中绘图的大小?

python - 隐藏和显示功能,如何解决?