问题
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.
回答1:
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.
回答2:
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.
来源:https://stackoverflow.com/questions/3189951/change-checkbox-position-for-a-qcheckbox