【问题标题】:How to format QRadioButton without label如何格式化没有标签的 QRadioButton
【发布时间】:2020-04-26 08:21:37
【问题描述】:

目前,单选按钮小部件的格式如下:

我想完全摆脱文本,并在它的小部件内居中显示指示器。所以它看起来像这样:

我需要做什么才能以这种方式格式化小部件?我正在使用编辑器,但我的某些代码块是手写的,所以我希望答案在两者之间起作用。谢谢!

【问题讨论】:

    标签: python pyqt pyqt5 qradiobutton


    【解决方案1】:

    您必须使用 QProxyStyle 来移动 QRadioButton 指示器的位置:

    import sys
    
    from PyQt5 import QtCore, QtWidgets
    
    
    class ProxyStyle(QtWidgets.QProxyStyle):
        def subElementRect(self, element, option, widget):
            r = super().subElementRect(element, option, widget)
            if element in (
                QtWidgets.QStyle.SE_RadioButtonIndicator,
                QtWidgets.QStyle.SE_RadioButtonFocusRect,
                QtWidgets.QStyle.SE_RadioButtonClickRect,
            ):
                r.moveCenter(option.rect.center())
            return r
    
    
    if __name__ == "__main__":
        app = QtWidgets.QApplication(sys.argv)
    
        app.setStyle(ProxyStyle())
    
        w = QtWidgets.QWidget()
        lay = QtWidgets.QHBoxLayout(w)
    
        for _ in range(3):
            btn = QtWidgets.QRadioButton()
            btn.setStyleSheet("background-color: palette(button);")
            btn.setSizePolicy(QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Minimum)
            lay.addWidget(btn)
    
        w.resize(320, 240)
        w.show()
    
        sys.exit(app.exec_())
    

    更新:

    在 Qt Designer 中无法观察到此更改,因此您必须使用代码,例如:

    ├── main.py
    └── mainwindow.ui
    

    ma​​inwindow.ui

    <?xml version="1.0" encoding="UTF-8"?>
    <ui version="4.0">
     <class>MainWindow</class>
     <widget class="QMainWindow" name="MainWindow">
      <property name="geometry">
       <rect>
        <x>0</x>
        <y>0</y>
        <width>800</width>
        <height>600</height>
       </rect>
      </property>
      <property name="windowTitle">
       <string>MainWindow</string>
      </property>
      <widget class="QWidget" name="centralwidget">
       <widget class="QRadioButton" name="radioButton">
        <property name="geometry">
         <rect>
          <x>130</x>
          <y>120</y>
          <width>221</width>
          <height>221</height>
         </rect>
        </property>
        <property name="styleSheet">
         <string notr="true">background-color: rgb(252, 233, 79);</string>
        </property>
        <property name="text">
         <string/>
        </property>
       </widget>
      </widget>
      <widget class="QMenuBar" name="menubar">
       <property name="geometry">
        <rect>
         <x>0</x>
         <y>0</y>
         <width>800</width>
         <height>24</height>
        </rect>
       </property>
      </widget>
      <widget class="QStatusBar" name="statusbar"/>
     </widget>
     <resources/>
     <connections/>
    </ui>
    

    ma​​in.py

    import os
    import sys
    
    from PyQt5 import QtCore, QtGui, QtWidgets, uic
    
    
    CURRENT_DIR = os.path.dirname(os.path.realpath(__file__))
    
    
    class ProxyStyle(QtWidgets.QProxyStyle):
        def subElementRect(self, element, option, widget):
            r = super().subElementRect(element, option, widget)
            if element in (
                QtWidgets.QStyle.SE_RadioButtonIndicator,
                QtWidgets.QStyle.SE_RadioButtonFocusRect,
                QtWidgets.QStyle.SE_RadioButtonClickRect,
            ):
                r.moveCenter(option.rect.center())
            return r
    
    
    if __name__ == "__main__":
    
        app = QtWidgets.QApplication(sys.argv)
        app.setStyle(ProxyStyle())
        w = uic.loadUi(os.path.join(CURRENT_DIR, "mainwindow.ui"))
        w.show()
        sys.exit(app.exec_())
    

    输出:

    注意:我给QRadioButton添加了背景色,以便区分位置。

    【讨论】:

    • 如何在构建器中实现这一点?
    • @Laif mmm,更好地解释一下自己,minimal reproducible example 会帮助我了解你
    • 我指的是 QT Designer 应用程序。它允许您通过 GUI 设计 UI 页面。我不能给你代码,因为它实际上不使用它。
    • @Laif 没错,我需要你提供 .ui 这是一个 XML,所以用任何文本编辑器打开 .ui 并将 XML 复制到你的问题中
    • 我还没有写任何东西,你想让我把基本的 XML 放在一个单选按钮上吗?
    猜你喜欢
    • 2020-03-17
    • 2011-12-18
    • 1970-01-01
    • 2015-04-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-12
    • 2021-09-30
    相关资源
    最近更新 更多