How to set QWidget width?
Asked Answered
M

4

37

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 aa;
aa.setWidth(165); //something like this?
Mays answered 3/6, 2010 at 14:40 Comment(0)
G
67

resize() might be better to use.

Example usage:

widget->resize(165, widget->height());
Gorga answered 3/6, 2010 at 14:42 Comment(1)
If content is larger than the widget, widget won't resize. In that case put all content on top of a QScrollarea. Then It'll resize and you can see the scrolling bars.Wisner
S
15
widget->resize(165, widget->height());
Selector answered 30/7, 2012 at 23:8 Comment(0)
B
6

QWidget reference.

Try examining all available "yyysize" methods (because there are different sizing policies for Qt widgets and you might need something special).

Basically, yes, it's resize(...).

Banksia answered 3/6, 2010 at 14:46 Comment(0)
H
6

If the width won't change afterwards, you can use setFixedWidth:

widget->setFixedWidth(165);

Similarly, to change the height without changing the width, there is setFixedHeight.

Heterogeneous answered 9/10, 2017 at 12:56 Comment(2)
Never use fixed width! For varius reselutions it will not work.Damiondamita
And you can't change your size in the future. Bad answer.Expeditious

© 2022 - 2024 — McMap. All rights reserved.