C++ 如何设置 QWidget 宽度?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2966952/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to set QWidget width?
提问by Narek
How to set QWidget
width? I know setGeometry(QRect& rect)
function to do that, but in that case I should use geometry()
function to get former parameters of my QWidget
, then I should increment the width and use setGeometry(..). Is there any direct way to that, say:
如何设置QWidget
宽度?我知道setGeometry(QRect& rect)
函数可以做到这一点,但在这种情况下,我应该使用geometry()
函数来获取我的前参数QWidget
,然后我应该增加宽度并使用 setGeometry(..)。有没有直接的方法,说:
QWidget aa;
aa.setWidth(165); //something like this?
回答by JimDaniel
回答by Petrucio
widget->resize(165, widget->height());
回答by M. Williams
Try examining all available "yyysize"methods (because there are different sizing policies for Qt widgets and you might need something special).
尝试检查所有可用的“yyysize”方法(因为 Qt 小部件有不同的大小调整策略,您可能需要一些特殊的东西)。
Basically, yes, it's resize(...)
.
基本上,是的,它是resize(...)
。
回答by Donald Duck
If the width won't change afterwards, you can use setFixedWidth
:
如果之后宽度不会改变,您可以使用setFixedWidth
:
widget->setFixedWidth(165);
Similarly, to change the height without changing the width, there is setFixedHeight
.
同样,要在不改变宽度的情况下改变高度,有setFixedHeight
.