Summary of problem:
Two of the three checkboxes above have checkmarks. In one, the value was set with Python code self.setwithPythoncode.setChecked(True)
whereas the other was set by the user clicking on the box while the app was running.
Actual Results: The background in the portion of the checkbox widget that is where the check goes is blue in the box checked by the user but it is plain (or white) on the one that was set in python code.
Desired Results: How can I change the code to make the background look the same when I set it programmatically as when it is set by the user, i.e., has the blue background in the actual box.
Discussion: BTW, if I check the "set with Python code" button twice, once uncheck it and again to check it, then the blue background appears.
What I've Tried: I haven't found a property of a QCheckBox or QAbstractButton that controls the background of just the checkable square. I couldn't find anything obvious in the Designer properties list for a checkbox.
Here is the Python code.
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_()
Here is 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>