【问题标题】:qpushbutton icon left alignment text center alignmentqpushbutton 图标左对齐文本居中对齐
【发布时间】:2019-10-01 10:05:58
【问题描述】:

我无法正确对齐图标和按钮文本。

我已经使用 Designer 生成了应用程序 gui,默认情况下它们看起来像这样:

我添加了一些代码,

win.pb_ejecutar.setStyleSheet("QPushButton { text-align: left; }")

我有这个

但我需要的是这个,图标左对齐和文本中心对齐

我已经通过在名称中添加空格来做到这一点,但我觉得它不是很优雅

有人帮我吗??谢谢

【问题讨论】:

    标签: python pyqt pyqt5 qpushbutton


    【解决方案1】:

    图标和文字的对齐方式是一样的,所以没有Qt Style Sheet的解决方案,所以另一种选择是使用QProxyStyle:

    import sys
    from PyQt5 import QtCore, QtGui, QtWidgets
    
    
    class ProxyStyle(QtWidgets.QProxyStyle):
        def drawControl(self, element, option, painter, widget=None):
            if element == QtWidgets.QStyle.CE_PushButtonLabel:
                icon = QtGui.QIcon(option.icon)
                option.icon = QtGui.QIcon()
            super(ProxyStyle, self).drawControl(element, option, painter, widget)
            if element == QtWidgets.QStyle.CE_PushButtonLabel:
                if not icon.isNull():
                    iconSpacing = 4
                    mode = (
                        QtGui.QIcon.Normal
                        if option.state & QtWidgets.QStyle.State_Enabled
                        else QtGui.QIcon.Disabled
                    )
                    if (
                        mode == QtGui.QIcon.Normal
                        and option.state & QtWidgets.QStyle.State_HasFocus
                    ):
                        mode = QtGui.QIcon.Active
                    state = QtGui.QIcon.Off
                    if option.state & QtWidgets.QStyle.State_On:
                        state = QtGui.QIcon.On
                    window = widget.window().windowHandle() if widget is not None else None
                    pixmap = icon.pixmap(window, option.iconSize, mode, state)
                    pixmapWidth = pixmap.width() / pixmap.devicePixelRatio()
                    pixmapHeight = pixmap.height() / pixmap.devicePixelRatio()
                    iconRect = QtCore.QRect(
                        QtCore.QPoint(), QtCore.QSize(pixmapWidth, pixmapHeight)
                    )
                    iconRect.moveCenter(option.rect.center())
                    iconRect.moveLeft(option.rect.left() + iconSpacing)
                    iconRect = self.visualRect(option.direction, option.rect, iconRect)
                    iconRect.translate(
                        self.proxy().pixelMetric(
                            QtWidgets.QStyle.PM_ButtonShiftHorizontal, option, widget
                        ),
                        self.proxy().pixelMetric(
                            QtWidgets.QStyle.PM_ButtonShiftVertical, option, widget
                        ),
                    )
                    painter.drawPixmap(iconRect, pixmap)
    
    
    if __name__ == "__main__":
    
        app = QtWidgets.QApplication(sys.argv)
        app.setStyle('fusion')
        proxy_style = ProxyStyle(app.style())
        app.setStyle(proxy_style)
    
        w = QtWidgets.QWidget()
        lay = QtWidgets.QVBoxLayout(w)
        icons = [
            app.style().standardIcon(standardIcon)
            for standardIcon in (
                QtWidgets.QStyle.SP_MediaPlay,
                QtWidgets.QStyle.SP_MediaPause,
                QtWidgets.QStyle.SP_MediaSeekBackward,
                QtWidgets.QStyle.SP_MediaSeekForward,
            )
        ]
        for text, icon in zip("Play Pause Backward Forward".split(), (icons)):
            button = QtWidgets.QPushButton(text)
            button.setIcon(icon)
            lay.addWidget(button)
        w.show()
        sys.exit(app.exec_())
    

    【讨论】:

      猜你喜欢
      • 2017-10-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-13
      • 2019-04-24
      • 2015-07-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多