How to make height of a QPushButton automatically fit the text?

白昼怎懂夜的黑 提交于 2021-02-10 14:21:52

问题


I have a QPushButton

QPushButton *btn = new QPushButton();
btn->setText("Push \n Button");

The result is:

I have tried btn->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred) with Qt Designer but it does not help.

How can do so that the height of the button is automatically adjusted to fit with the content when I call setText?


回答1:


You can set the vertical size policy to an option that allows the widget to grow, the default option for vertical size policy of QPushButton is fixed, you can do it like this from code:

btn->setText("Push \n Button");
btn->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);

You can change it in designer, if you use that to create your ui. See more options in the documentation here and choose the one that best suits your needs.

Also use layouts for your widgets, those help a lot, especially if your windows can change size.



来源:https://stackoverflow.com/questions/58728873/how-to-make-height-of-a-qpushbutton-automatically-fit-the-text

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