Background color of QCheckBox differs when checked by user vs when python code sets it checked

100
December 09, 2019, at 5:20 PM

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>
Rent Charter Buses Company
READ ALSO
How to make button change colour in tkinter?

How to make button change colour in tkinter?

How do I make a button change colour in python tkinter 3? I saw one using the self code but I don't like to use thatHere is the code:

109
How to make code work without import anything [closed]

How to make code work without import anything [closed]

Want to improve this question? Update the question so it focuses on one problem only by editing this post

99
Class as first argument for single dispatch

Class as first argument for single dispatch

As the documentation states the dispatch happens on the type of the first non-self or non-cls argumentIn this way, I'm trying to apply some function to a pandas DataFrame, which I would like it to be generic

122
Handling API Pagination, using a &#39;next.link" object

Handling API Pagination, using a 'next.link" object

I am using the odatanextLink found at the bottom of certain requests to handle pagination

98