Change checkbox position for a QCheckBox

I have a QCheckbox in a grid layout defined as such:

self.isSubfactorCheckbox = QtGui.QCheckBox('Is &subfactor', self)

By default the checkbox is to the left of the "Is subfactor" text. I wish to move it to the right of the text.

I've tried playing around with subcontrol-position but to no avail.

self.isSubfactorCheckbox.setStyleSheet('QCheckBox::indicator{subcontrol-origin: content; subcontrol-position: top right;}')

This moves the checkbox to the right (it's still on the left of the text) but it pushes the text to the right and out of the window's edge.

This didn't help to move the text to the right:

self.isSubfactorCheckbox.setStyleSheet('QCheckBox::text{subcontrol-origin: content; subcontrol-position: top left; }')

A solution would be creating a QLabel and adding the checkbox on its right but I don't haven't found a way to underline letters in a QLabel so that the user will know which is the shortcut key. I've tried prefixing letters with & or wrapping them in <u> </u> tags.

I'd still prefer to use only a QLabel, but how would I switch the checkbox to the right of the text without the text getting pushed out.

Answers


Use the buddy mechanism. Set the QCheckBox to the buddy of the QLabel. After that prefix & will underline the shortcut character in the label. See the documentation of the QLabel's setBuddy.


Even easier...

Use the QWidget.setLayoutDirection method and set it RightToLeft vs. LeftToRight

You can do this in Designer or through code, it is a property or all widgets.


Need Your Help

Is it necessary to use a std::atomic to signal that a thread has finished execution?

c++ c++11 stdthread stdatomic

I would like to check if a std::thread has finished execution. Searching stackoverflow I found the following question which addresses this issue. The accepted answer proposes having the worker thre...

Strange behavior of static global variable

c++ static global

I know that this program is not using the static variable in an appropriate way, but it shows how to reproduce a behavior I have seen :