本文介绍了如何将 QPushButton 设置为垂直增长?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

背景:我需要 命令链接 之类的控件.通常,我会使用 Qt 的 内置 QCommandLinkBut​​ton,但在这种情况下,我还需要支持从右到左的布局.遗憾的是,当通过 setLayoutDirection 显式设置时,QCommandLinkBut​​ton 似乎不尊重 Qt::RightToLeft.似乎也没有办法通过样式表覆盖布局方向.

Background: I need Command Link-like controls. Normally, I would use Qt's builtin QCommandLinkButton, but in this case I need to also support Right-to-Left layouts. Regrettably, QCommandLinkButton doesn't seem to respect Qt::RightToLeft when set explicitly via setLayoutDirection. There also doesn't seem to be a way to override the layout direction via style sheets.

我尝试通过使用 QPushButton 来解决这个问题:我实例化了 QPushButton 并将其内容替换为包含两个垂直堆叠标签的自定义布局.这不起作用,因为 QPushButton 拒绝水平扩展.

I tried to work around this issue by using QPushButton: I instantiated QPushButton and replaced its contents with a custom layout containing two labels stacked vertically. This did not work because QPushButton refused to expand horizontally.

未能使 QPushButton 实例工作,我决定尝试对其进行子类化.无论我尝试过什么(覆盖 sizeHint,将垂直 sizePolicy 设置为展开,resize 显式等),按钮都拒绝增长超过 30像素高.

Failing to make QPushButton instances work, I decided to try sub-classing it. No matter what I tried (overriding sizeHint, setting vertical sizePolicy to expanding, resizeing explicitly, etc) the button refuses to grow more than 30 pixels high.

如果我将 QPushButton 更改为 QWidget,新的小部件会正常增长.

If I change QPushButton to QWidget, the new widget grows normally.

问题:如何强制 QPushButton 垂直增长并适合其内部小部件?如果这是不可能的,对于与 QPushButton 和其他本机"控件在视觉和行为上均一致的类似按钮的小部件,我有哪些选择?

The question: How can I force a QPushButton to grow vertically and fit its inner widgets? If this is not possible, what are my options for a button-like widget that is both visually and behaviorally consistent with QPushButton and other "native" controls?

推荐答案

我通过将最大高度设置为 16777214(而不是默认的 16777215),然后设置按钮的内容边距,设法让按钮正常增长到 10.

I managed to get the button to grow normally by setting the maximum height to 16777214 (instead of the default 16777215), and then setting the content margins of the button to 10.

令人困惑的是,QPushButton 的默认最大高度应该足够了,但由于某种原因它拒绝增长超过 30 像素,除非最大高度明确设置为其他.

Perplexingly, the default maximum height of a QPushButton should be plenty, but for some reason it refuses to grow past 30 pixels unless the maximum height is explicitly set to something else.

这篇关于如何将 QPushButton 设置为垂直增长?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-31 14:33