水平中央单选按钮标签

问题描述 投票:0回答:1

我正在使用单选按钮创建垂直标签效果。我完成了所有工作,但是我坚信自己一定很容易做。

在代码的这一部分中,我浏览了选项卡名称的列表,并为每个选项卡生成了单选按钮:

def _init_vertical_tabs(self, layout, row):
    """Initialize vertical tabs"""
    tabs_widget = QtWidgets.QWidget()
    tabs_widget.setObjectName("tabs")
    vertical_tabs_layout = QtWidgets.QVBoxLayout()
    vertical_tabs_layout.setSpacing(0)
    vertical_tabs_layout.setContentsMargins(0, 0, 0, 0)
    vertical_tabs_layout.setAlignment(QtCore.Qt.AlignCenter)
    tabs_widget.setLayout(vertical_tabs_layout)
    layout.addWidget(tabs_widget, row, 0, len(self.tabs), 1)

    tabs_widget.setMinimumHeight(self.parent_height)
    tabs_widget.setMinimumWidth(self.parent_width*0.15)
    tabs_widget.setMaximumHeight(self.parent_height)
    tabs_widget.setMaximumWidth(self.parent_width*0.15)

    tab_to_widget = dict()
    for tab in self.tabs:
        tab_button = self._create_tab(tab, vertical_tabs_layout, self.parent_width*0.15, self.parent_height/len(self.tabs),)
        tab_to_widget[tab] = tab_button

def _create_tab(self, tab, layout, width, height):
    """Basic method to create a fake tab"""
    button = QtWidgets.QRadioButton(tab)
    button.setContentsMargins(0, 0, 0, 0)
    l = lambda tab_name = tab: self.toggle_visibility_tabs(tab_name)
    button.clicked.connect(l)

    button.setMinimumHeight(height)
    button.setMinimumWidth(width)
    button.setMaximumHeight(height)
    button.setMaximumWidth(width)

    layout.addWidget(button)

然后我应用了一些CSS:

    QWidget#tabs{
  color: #ffffff;
  background-color: #005073;
  border: 0px solid #e54d42;
  text-align: center;
}

QWidget#tabs QRadioButton{
  color: #ffffff;
  font-weight: 400;
  font-size: 20pt;
  border-radius: 0px;
  background-color: #003850; /*QLinearGradient(x1:0, y1:0, x2:0, y2:1, stop:0 #003850, stop:0.5 #005073, stop:1 #003850);*/
  margin: 0px;
  font-family: Arial, Helvetica, sans-serif;
  text-align: center;
}

QWidget#tabs QRadioButton#Validator{
  border-bottom-left-radius: 60px;
  border-left: none;
}

QWidget#tabs QRadioButton::indicator{
  width: 0px;
  height: 0px;
  border-radius: 0px;
  border: 1px solid #e54d42;
  border-top: none;
  border-right: none;
  border-left: none;
  font-family: Arial, Helvetica, sans-serif;
  text-align: center;
}

QWidget#tabs QRadioButton:hover, QWidget#tabs QRadioButton:selected,  QWidget#tabs QRadioButton:checked{
  border-left: 30px solid #e54d42;
  color: #e54d42;
}

结果为:enter image description here

我想将文本放在中间,我尝试使用尺寸策略,但我做到了:enter image description here

但这不是我想要的,首先单选按钮标签仍然左对齐,并且由于单选按钮较短,因此所选单选按钮的红色边框显然不再位于左侧。有人可以告诉我在CSS或python代码中是否有实现此目的的简便方法?我也尝试过text-align,但也没有用。

谢谢!

python pyqt pyqt5 qtstylesheets
1个回答
1
投票

仅使用样式表几乎是不可能的。我建议您混合使用样式表自定义绘画。

首先,编辑样式表,确保单选按钮的all颜色设置为transparent

然后创建QRadioButton的子类,它们都通过调用基类实现来绘制按钮。and绘制具有指定颜色和字体的文本。

class CustomRadio(QtWidgets.QRadioButton):
    def paintEvent(self, event):
        # draw the widget as paintEvent normally does
        super().paintEvent(event)

        # create a new painter on the widget
        qp = QtGui.QPainter(self)
        # create a styleoption and init it with the button
        opt = QtWidgets.QStyleOptionButton()
        self.initStyleOption(opt)

        # now we can know if the widget is hovered and/or checked
        if opt.state & (QtWidgets.QStyle.State_MouseOver | QtWidgets.QStyle.State_On):
            # if it is, set the color accordingly
            qp.setPen(QtGui.QColor('#e54d42'))
        else:
            qp.setPen(QtCore.Qt.white)
        # finally, draw the text
        qp.drawText(self.rect(), 
            QtCore.Qt.AlignCenter | QtCore.Qt.TextShowMnemonic, self.text())

[如果您使用的是Designer,则需要升级微调框:

  • 选择所有单选按钮,右键单击其中任何一个,然后从上下文菜单中选择Promote to...
  • 确保“基类名称”组合框设置为“ QRadioButton”;
  • 在“升级的类名称”中,编写“ CustomRadio”(上面的子类的名称);
  • 在“标题文件”字段中,输入保存子类的文件名,不带扩展名(例如,如果使用main.py,则写“ main”);请记住,该路径是相对于ui文件(或py文件,如果您使用的是pyuic)的路径,因此,如果ui / py位于程序的主文件夹中,则子类文件位于“ subclasses”目录中并命名为“ myradios.py”,则必须在此处编写“ subclasses.myradios”;
  • 单击“添加”以创建新的升级的类,然后单击“升级”以实际升级所选的收音机;
  • 如果您以前没有选择所有广播,现在可以使用上下文菜单的Promote to子菜单中的新“自定义广播”项来促销它们;

保存ui文件,如果使用pyuic,则生成新的python文件,就可以了。

© www.soinside.com 2019 - 2024. All rights reserved.