Qt World Summit: Register Today!

How to resize QWidget to fit its contents?

  • Hi,

    Basically I have a QTableWidget inside a QVBoxLayout. I set everything so that my table widget resizes to fit the contents. This is good, but I can't get my QWidget that hosts this table widget to resize itself to show everything.

    I checked tablewidget.size() but the size is not the correct size, because if I use the same size for my QWidget, the table is cropped, i.e. I can't see it fully.

    How can I achieve this?

    Thanks in advance.

  • try tableWidget->setSizeAdjustPolicy(QAbstractScrollArea::AdjustToContents);

  • @VRonin Thanks a lot, you are right it works :) I have another issue about the row heights, I am trying to resize them but they don't seem to get smaller in height than this:

    alt text

    I am doing it like this:
    hh = tableWidget.horizontalHeader()

        vh = tableWidget.verticalHeader()
    I don't understand why row heights refuse to get smaller.

  • Lifetime Qt Champion

    There is
    and setDefaultSectionSize won't go under that value
    so you must lower that first to go really low.

  • @mrjj said in How to resize QWidget to fit its contents?:


    Thanks a lot I just tried that but no luck, and if I remove default section size function call, then they get larger:


    This still looks same size for example.

  • When I used this it worked: vh.setSectionResizeMode(QtWidgets.QHeaderView.Stretch)

Log in to reply