Important: Please read the Qt Code of Conduct -

How To make A parent Widget Re-Size To Size Of Child Widget

  • I will apologize up front for my Qt ignorance.
    I am a Win32 programmer jumping into Qt and have been
    pounding away at it for 2 weeks.

    I believe my 'problem' is simple.
    Perhaps my lack of understanding Qt nomenclature has hindred me
    from authoring the 'correct' search string!

    All I want to do is to have a QDialog size itself to the 'width'
    of a child QTableWidget.

    MyDialog : public QDialog {

    Layout=new QVBoxLayout()

    ToolBar=new QToolBar()
    PathLabel=new QLabel()
    Table=new MyTable()




    MyTable : public QTableWidget {


    MyDialog looks fine save for MyTable.
    MyTable has six columns that seem to resize themselves
    based on the contents of the rows (good!).

    All six columns cannot be displayed within the width of MyDialog
    so there is a horizontal scroll bar on MyTable.

    I want MyDialog to be just wide enough to display all six
    columns of MyTable (i.e. resize itself to the width of MyTable).

    When I re-populate the contents of MyTable (the columns will resize
    in width) I want MyDialog again to resize it's width (maybe grow,
    maybe shrink).

    I cannot for the life of me figure out how to do this!
    It seems to me at this point in time that this would be a very
    common thing to do and the functionality must be present within Qt.

    Thanks in advance for any help.

  • Hi @RiverDingo ,

    please go through this link , this might help .

  • Are you able to acquire the size of the table widget after you populate?

    Does Table.size().width() return the width of the table within the scroll area or does it return the width of the scroll area?

    Basically to me you need to figure out the net width/height of your table and use the setMinimum(height or width) and setMaximum(Height or width) on your dialogue window. I am still pretty new to Qt as well, but when i need to fix the size of something I will often set max and min sizes to lock them into a certain size. At that point a setGeometry may be better so that you can make sure things are centered or whatever. What is not clear to me right now is how you should get the dimensions of the table itself. If Table.size().width() works then that would be really nice but not sure it is that easy.

  • Thanks surajit_sen.
    I have already read through the layout description several times to no avail.

    I understand what you are saying, but have found no way of obtaining the width of MyTable. I have overriden the sizeHint(), ReSizeEvent(),... and all of the values are either -1 (initially) or the actual width of MyDialog (at least the client area within the layout).

    I really believe that this has something to do with MyTable (again, derived from QTableWidget).

    ToolBar=new QToolBar()
    PathLabel=new QLabel()
    Table=new MyTable()



    MyDialog will grow ('widen') to accommodate the length of PathLabel. Put 100
    charcters in PathLabel then MyDialog displays all 100. At runtime PathLabel only contains around 20 characters.

    So my problem is how to get MyDialog (or probably the Layout) to recognize the width of MyTable?
    Note that if I resize MyDialog (via mouse) and make it wider I see the other columns, eventually I get wide enough to where the horizontal scroll bar on MyTable goes away. Note also that if I shrink the width of MyDialog (via mouse) the scroll bar re-appears. It seems like MyTable has a 'fixed' width but I am unable to find it.

    Thanks all so far.

  • Update (Some progress).

    I added (setSizeAdjustPolicy)(QAbstractScrollArea::AdjustToContents) to my initialization.

    • When MyDialog is first displayed all columns of MyTable are showing. (i.e. MyDaalog has increased it width). No horizontal scroll bas is displayed.


    • I repopulate MyTable. On of the columns fields (Item) is now wider than before.
    • MyDialog does not widen to accommodate the new width (the horizontal scroll bas appears)
    • I see in my sizeHint() that MyTable is actually reporting itself as wider.

    I am still at a loss!


Log in to reply