在右侧对齐图标并将文本居中放置在 QPushButton 中 [英] Align icon on the right and center the text in a QPushButton

查看:415
本文介绍了在右侧对齐图标并将文本居中放置在 QPushButton 中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将图标向右对齐并将文本居中放置在 QPushButton 中,但我对目前的结果并不满意.我还希望图标完全位于 QPushButton 的右侧,而不是与文本居中.同样,我希望它可以使用位于右侧或左侧的图标,同时能够选择按钮的宽度.你有解决方案吗 ?你能帮助我吗 ?这是我现在的代码:

I'm trying to align the icon to the right and center the text in a QPushButton, but I'm not satisfied with the result for now. I would also like the icon to be completely to the right of the QPushButton and not centered with the text. Likewise I would like this to work with an icon positioned to the right or left while being able to choose the width of the button. Do you have a solution ? Can you help me ? Here is my code for now:

# Les boutons du bas pour passer d'une page à l'autre
bout_pag_charg_enr_phase_1 = QPushButton("Aller directement à la page de maintenance de la Séquence")
bout_pag_charg_enr_phase_1.setFixedWidth(406)
bout_pag_charg_enr_phase_1.setIcon(QIcon('logo_png_apsc'+os.sep+'icone_3_fleches_droite_apsc_256x256.png'))
bout_pag_charg_enr_phase_1.setIconSize(QSize(16, 16))
bout_pag_charg_enr_phase_1.setLayoutDirection(Qt.RightToLeft)
bout_pag_charg_enr_phase_1.setStyleSheet("text-align: center; background-color: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1, stop: 0 #fdfbf7, stop: 1 #6190F2);border-style: solid;border-width: 2px;border-radius: 8px;border-color: #9BB7F0;padding: 3px")
bout_pag_suivante_phase_1 = QPushButton("Page suivante")
bout_pag_suivante_phase_1.setFixedWidth(120)
bout_pag_suivante_phase_1.setIcon(QIcon('logo_png_apsc'+os.sep+'icone_2_fleches_droite_apsc_256x256.png'))
bout_pag_suivante_phase_1.setIconSize(QSize(16, 16))
bout_pag_suivante_phase_1.setLayoutDirection(Qt.RightToLeft)
bout_pag_suivante_phase_1.setStyleSheet("background-color: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1, stop: 0 #fdfbf7, stop: 1 #6190F2);border-style: solid;border-width: 2px;border-radius: 8px;border-color: #9BB7F0;padding: 3px")

我得到了什么:

我想要的:

推荐答案

一个可能的解决方案是在顶部和右侧放置一个显示图标的 QLabel.

A possible solution is to place on top and on the right side a QLabel that shows the icon.

import os
from PyQt5 import QtCore, QtGui, QtWidgets

class PushButton(QtWidgets.QPushButton):
    def __init__(self, *args, **kwargs):
        super(PushButton, self).__init__(*args, **kwargs)
        self.setStyleSheet('''background-color: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1, stop: 0 #fdfbf7, stop: 1 #6190F2);
            border-style: solid;border-width: 2px;
            border-radius: 8px;
            border-color: #9BB7F0;padding: 3px;''')

        icon = self.icon()
        icon_size = self.iconSize()
        # remove icon
        self.setIcon(QtGui.QIcon())
        label_icon = QtWidgets.QLabel()
        label_icon.setAttribute(QtCore.Qt.WA_TranslucentBackground)
        label_icon.setAttribute(QtCore.Qt.WA_TransparentForMouseEvents)
        lay = QtWidgets.QHBoxLayout(self)
        lay.setContentsMargins(0, 0, 0, 0)
        lay.addWidget(label_icon, alignment=QtCore.Qt.AlignRight)
        label_icon.setPixmap(icon.pixmap(icon_size))

class MyWindow(QtWidgets.QWidget):
    def __init__(self, parent=None):
        super(MyWindow, self).__init__(parent)

        icon = QtWidgets.QApplication.style().standardIcon(QtWidgets.QStyle.SP_ArrowRight)
        # icon = QtGui.QIcon(os.path.join('logo_png_apsc', 'icone_3_fleches_droite_apsc_256x256.png'))

        bout_pag_charg_enr_phase_1 = PushButton("Aller directement à la page de maintenance de la Séquence",
            icon=icon, iconSize=QtCore.QSize(16, 16))
        bout_pag_charg_enr_phase_1.setFixedWidth(506)

        bout_pag_suivante_phase_1 = PushButton("Page suivante",
            icon=icon, iconSize=QtCore.QSize(16, 16))
        bout_pag_suivante_phase_1.setFixedWidth(140)

        lay = QtWidgets.QHBoxLayout(self)
        lay.addWidget(bout_pag_charg_enr_phase_1)
        lay.addWidget(bout_pag_suivante_phase_1)        

if __name__ == '__main__':
    import sys
    app = QtWidgets.QApplication(sys.argv)
    main = MyWindow()
    main.show()
    sys.exit(app.exec_())

这篇关于在右侧对齐图标并将文本居中放置在 QPushButton 中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆