35

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?
Tomáš Zato
  • 50,171
  • 52
  • 268
  • 778
Narek
  • 38,779
  • 79
  • 233
  • 389

4 Answers4

67

resize() might be better to use.

Example usage:

widget->resize(165, widget->height());
sashoalm
  • 75,001
  • 122
  • 434
  • 781
JimDaniel
  • 12,513
  • 8
  • 61
  • 67
  • 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. – Hareen Laks Apr 16 '20 at 03:29
15
widget->resize(165, widget->height());
Petrucio
  • 5,491
  • 1
  • 34
  • 33
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(...).

M. Williams
  • 4,945
  • 2
  • 26
  • 27
5

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.

Donald Duck
  • 8,409
  • 22
  • 75
  • 99