0

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

在此处输入图像描述

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

在此处输入图像描述

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

4

1 回答 1

0

您必须使用 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

主窗口.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>

主文件

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 添加了背景颜色,以便可以区分位置。

于 2020-01-08T19:19:18.527 回答