QCheckbox/QRadioButton 换行
我正在尝试使用标准 QCheckbox/QRadioButton 与 Qt 建立多行复选框/单选按钮。
我没有找到直接的解决方案,因为 QRadioButton{wrap:true;}
没有效果。 唯一可能的就是访问 QRadioButton->label->setLineWrap(true) 但
- 我想从设计者那里做到这一点,
- 而不必重写小部件
除了放置之外还有什么想法QRadioButton 和 QLabel 彼此相邻?
谢谢,鲍里斯。
I'm trying to have a multi-line checkbox/radiobutton with Qt using standard QCheckbox/QRadioButton.
I didn't find the direct solution since QRadioButton{wrap:true;}
has no effect.
The only thing possible would be to access to the QRadioButton->label->setLineWrap(true)
but
- I'd like to do that from the designer
- not having to rewrite a widget
Any idea beside putting a QRadioButton and a QLabel next to each others?
Thx, Boris.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我的解决方案:
My solution:
这确实很烦人,如果不重新实现就无法解决:如果我没记错的话,标签使用
Qt::PlainText
。在我们的项目中,UI 团队通过两种方法解决了这个问题。使用 QRadioButton 按钮作为标题
以仅包含该选项的简短描述的方式重写 QRadioButton 文本。在其下方放置一个 QLabel 并添加更长的描述。我们使用较小的字体和一点缩进,使其看起来整洁。例如,这在 Mac OS X 中经常使用。
从单选按钮中删除文本
重新布局 UI,以便将每个单选按钮放置在 QLabel 的左侧。将整个文本添加到 QLabel 并将标签设置为单选按钮的伙伴。这是功能最少的方法,因为单击标签不会选中单选按钮。另外,对齐也不是很好。
That indeed is really annoying and cannot be solved without reimplementation: the label uses
Qt::PlainText
if I'm not mistaken. In our projects, the UI team solved this with two approaches.Using the QRadioButton button as a title
Rewrite the QRadioButton text in such a fashion that it has only a brief description of the option. Put a QLabel underneath it and add a longer description. We use a smaller font and a little indentation to make it look neat. This is used frequently in Mac OS X, for example.
Removing the text from the radio button
Relayout the UI so that each radio button is put on the left of a QLabel. Add the whole text to the QLabel and set the label as the radio buttons' buddy. This is the least functional approach, because clicking the label does not check the radio button. Also, alignment is not very good.
我知道的唯一方法(但它不是“可布局的”)是将 \n 符号放入字符串中。
The only way I know (but it's not "layoutable") is putting \n sign into a string.
问题
不幸的是,对此没有简短的解决方案。有一个关于此的 Qt 功能请求(请参阅 QTBUG-5370),但此后一直处于待处理状态2009 年(!),所以它可能永远不会实现。
然而,实际上可以通过重写 QRadioButton::resizeEvent 并在那里执行必要的换行来实现这一点。除此之外,我们还必须调整水平尺寸策略并覆盖最小尺寸提示,以便单选按钮的布局正确。
下面给出了完整的实现。
LineWrappedRadioButton.h
LineWrappedRadioButton.cpp
main.cpp
这将创建一个可自由调整大小的窗口,其中包含一个换行单选按钮:
The problem
There unfortunately is no short and simple solution for this. There's a Qt feature request about this (see QTBUG-5370), but it's pending since 2009 (!), so it probably will never be implemented.
However, it's actually possible to implement this by overriding the
QRadioButton::resizeEvent
and to perform the necessary line wrapping there. In addition to that, we must also adjust the horizontal size policy and override the minimum size hint so that the layout of the radio button will be correct.The complete implementation is given below.
LineWrappedRadioButton.h
LineWrappedRadioButton.cpp
main.cpp
This will create a freely resizable window with a line wrapped radio button in it:
我成功添加了布局和标签作为单选按钮的子项,并将垂直大小策略更改为“首选”(而不是“固定”)。
不幸的是,这并没有像本机标签一样自动使其对鼠标悬停和单击做出反应,但请注意一下黑客:我为单选按钮设置了样式表(“border:none”),它开始工作。也许这是幕后发生的一些功能;我很想知道一些确定性。
并且可以使用“QRadioButton::indicator{subcontrol-position:top left}” <- http://doc.trolltech.com/qq/qq20-qss.html
I succeeded adding a layout and a label as a child for the radio button, and changing the vertical size policy to Preferred (instead of Fixed).
Unfortunately this didn't automatically make it react to the mouse hovers and clicks like the native label, but watch a hack: I setStyleSheet("border:none") for the radio button and it started working. Maybe that's some feature happening behind the scenes; I'd love to have some certainty.
And the indicator can be aligned using "QRadioButton::indicator{subcontrol-position:top left}" <- http://doc.trolltech.com/qq/qq20-qss.html