[Solved] Resize QDialog to fit all contents?



  • Hi,
    I have a QDialog subclass in which there is a vertical layout.
    There are 3 widgets aligned vertically inside it.
    The middle widget is a QScrollArea to which I am setting a grid layout.
    The grid layout contains QTableView objects which I am creating by code using the Model/View framework.
    The number of QTableView in the grid layout is dynamic in the sense that user can change it through the UI.
    By default the grid layout contains only 1 QTableView & the size of the dialog is sufficient to fit all the contents.
    But when the user changes the number to 2 then the size of dialog is not changing to accommodate the 2 QTableView's.
    As a result the QTableView are creating horizontal scroll bars, which I don't want.
    I want the dialog to resize such that all the columns of all the QTableView's are always visible.
    How do I do this?



  • This issue is really bugging me.
    Can I please have some guidance on this?



  • It would really help me if somebody could answer this question?
    Am I unclear in my question?


  • Moderators

    Hi,

    Calculate the "total width of all columns":http://qt-project.org/doc/qt-5.1/qtwidgets/qtableview.html#columnWidth, and then "resize your dialog":http://qt-project.org/doc/qt-5.1/qtwidgets/qwidget.html#size-prop accordingly.

    Similarly, resize your dialog when the number of views is changed.



  • Hi JKSH,
    Thanks for your reply.
    I think I totally missed that... ;)
    But I was hoping something like QDialog::resizeToContents();


  • Moderators

    You're welcome :)

    resizeToContents() would create a circular dependency -- a child widget's size depends on its parent's size, so we can't make the parent's size depend on the child's size too.



  • ohh...
    Got it!



  • very similar problem i have a QTableWidget instead of a QTableView

    and resizing QTableWidget to rows and columns does not increase QDialog size, only increases QTableWidget size.

    help me increase QDialog size also

    plz help me



  • i can't now tell about qDialog, but there is "resizeToContents" with the help of the widget.

    1. Create widget (for example: mainWidget);
    2. specify widget layout;
    3. add some additional widgets inside layout.
    4. mainWidget."adjustSize()":http://qt-project.org/doc/qt-5/qwidget.html#adjustSize
    5. Enjoy.

    If it is necessary, i can post example code tomorrow.



  • This might work:

    Create your own class that inherits QTableView. Now QTableView has a protected virtual method it inherits from QAbstractScrollArea called viewportEvent ( QEvent * event ). I think you can can a contents resize event of the viewport judging by the 4.8 docs. So once you get the size you can emit a signal to the dialog to resize itself.

    In the resizing of the QDialog you may need to disconnect or set a flags in our QTableview so as not to get the circular calling: QTable::resize->QDialog::resize->QTable::resize



  • hmmm....
    Thanks for the reply, ill try it and post back


Log in to reply