How can I set a QPushButton to grow vertically?

半城伤御伤魂 提交于 2020-06-01 06:20:47

问题


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.

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.

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.

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

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?


回答1:


Place the button inside a layout and set its vertical size policy to Maximum.




回答2:


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.

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.



来源:https://stackoverflow.com/questions/58537802/how-can-i-set-a-qpushbutton-to-grow-vertically

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!