Change checkbox position for a QCheckBox

2020-04-08 14:38发布

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.

2条回答
男人必须洒脱
2楼-- · 2020-04-08 14:59

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.

查看更多
狗以群分
3楼-- · 2020-04-08 15:19

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.

查看更多
登录 后发表回答