【发布时间】:2019-12-09 02:00:35
【问题描述】:
问题总结:
上面三个复选框中有两个带有复选标记。一种是使用 Python 代码self.setwithPythoncode.setChecked(True)设置值,而另一种是由用户在应用运行时单击框设置的。
实际结果: 复选框小部件部分的背景在用户选中的框中是蓝色的,但在 python 代码中设置的那个是纯色(或白色)。
期望的结果: 当我以编程方式设置它时如何更改代码以使背景看起来与用户设置时相同,即在实际框中具有蓝色背景。
讨论: 顺便说一句,如果我两次选中“使用 Python 代码设置”按钮,一次取消选中,然后再次选中它,则会出现蓝色背景。
我的尝试: 我还没有找到 QCheckBox 或 QAbstractButton 的属性来控制可检查方块的背景。我在复选框的设计器属性列表中找不到任何明显的内容。
这是 Python 代码。
from PyQt5.QtWidgets import QApplication
from PyQt5 import QtWidgets, uic
class X(QtWidgets.QTableWidget):
def __init__(self, ui_file):
super(X, self).__init__()
uic.loadUi(ui_file, self)
self.setwithPythoncode.setChecked(True)
if __name__== '__main__':
app = QApplication([''])
window = X("test_check_boxes.ui")
window.show()
app.exec_()
这里是 test_check_boxes.ui
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>Form</class>
<widget class="QWidget" name="Form">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>217</width>
<height>201</height>
</rect>
</property>
<property name="windowTitle">
<string>Form</string>
</property>
<widget class="QCheckBox" name="setbyuserclicking">
<property name="geometry">
<rect>
<x>10</x>
<y>122</y>
<width>161</width>
<height>18</height>
</rect>
</property>
<property name="sizePolicy">
<sizepolicy hsizetype="Fixed" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="minimumSize">
<size>
<width>30</width>
<height>0</height>
</size>
</property>
<property name="focusPolicy">
<enum>Qt::NoFocus</enum>
</property>
<property name="autoFillBackground">
<bool>false</bool>
</property>
<property name="text">
<string>Set by user clicking</string>
</property>
<property name="checked">
<bool>false</bool>
</property>
</widget>
<widget class="QCheckBox" name="notsetanywhere">
<property name="geometry">
<rect>
<x>10</x>
<y>18</y>
<width>141</width>
<height>18</height>
</rect>
</property>
<property name="sizePolicy">
<sizepolicy hsizetype="Fixed" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="minimumSize">
<size>
<width>30</width>
<height>0</height>
</size>
</property>
<property name="focusPolicy">
<enum>Qt::NoFocus</enum>
</property>
<property name="text">
<string>Not set anywhere</string>
</property>
</widget>
<widget class="QCheckBox" name="setwithPythoncode">
<property name="geometry">
<rect>
<x>10</x>
<y>70</y>
<width>181</width>
<height>18</height>
</rect>
</property>
<property name="sizePolicy">
<sizepolicy hsizetype="Fixed" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="minimumSize">
<size>
<width>30</width>
<height>0</height>
</size>
</property>
<property name="focusPolicy">
<enum>Qt::NoFocus</enum>
</property>
<property name="text">
<string>Set with Python code</string>
</property>
<property name="checked">
<bool>false</bool>
</property>
</widget>
</widget>
<resources/>
<connections/>
</ui>
【问题讨论】:
-
我怀疑这个蓝色是在最后一个 QCheckBox 中给出的,因为它有焦点。如果单击“未在任何地方设置”,第三个 QCheckBox 仍然是蓝色?
-
是的。通过选中该框设置颜色后,在我选中另一个框后它保持设置。
-
感谢 @eyllanesc 添加我忘记复制的导入语句。如果有人在看这个,他们应该一定会看到他的编辑。
标签: python pyqt pyqt5 qcheckbox