How to set the preferred width when create a widget?



  • i just found the API to set minWidth/maxWidth. but i just want to set the width when the width is created...
    eg. i add a listbox or a tablewidget in the QDockWidget.


  • Moderators

    I am not sure I understand you well. So I'll post some general info and maybe you will find something useful in it;

    The preferred way of UI creation in QtWidgets is to use layouts. Layouts are responsible for correct positioning and sizing of all their widgets - there is no need to set any sizes by hand

    You can modify how a widget behaves in a widget by setting it's SizePolicy, min/ max size. You can also use Spacers to push widgets in layouts

    If you really want to change the size by hand, you can use setGeometry(), resize() or setFixedSize() methods

    If you want to know a nice "default" size for a widget, just use the sizeHint():

    @
    widget.setSize(widget.sizeHint());
    @


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.