简体   繁体   English

更改QCheckBox的复选框位置

[英]Change checkbox position for a QCheckBox

I have a QCheckbox in a grid layout defined as such: 我在如下定义的网格布局中有一个QCheckbox:

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. 一种解决方案是创建QLabel并在其右侧添加复选框,但我还没有找到在QLabel中的字母下划线的方法,以便用户知道哪个是快捷键。 I've tried prefixing letters with & or wrapping them in <u> </u> tags. 我试过用&前缀字母或将它们包装在<u> </u>标记中。

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. 我仍然希望仅使用QLabel,但是如何在不弹出文本的情况下将复选框切换到文本右侧。

Even easier... 更容易...

Use the QWidget.setLayoutDirection method and set it RightToLeft vs. LeftToRight 使用QWidget.setLayoutDirection方法并将其设置为RightToLeft与LeftToRight

You can do this in Designer or through code, it is a property or all widgets. 您可以在Designer中或通过代码执行此操作,它是属性或所有小部件。

Use the buddy mechanism. 使用伙伴机制。 Set the QCheckBox to the buddy of the QLabel. 将QCheckBox设置为QLabel的好友。 After that prefix & will underline the shortcut character in the label. 在该前缀之后,&将在标签中的快捷方式字符下划线。 See the documentation of the QLabel's setBuddy . 请参阅QLabel的setBuddy的文档。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM