Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [Solved] Resize QDialog to fit all contents?
QtWS25 Last Chance

[Solved] Resize QDialog to fit all contents?

Scheduled Pinned Locked Moved General and Desktop
12 Posts 6 Posters 30.2k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • CAD_codingC Offline
    CAD_codingC Offline
    CAD_coding
    wrote on last edited by
    #1

    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?

    1 Reply Last reply
    0
    • CAD_codingC Offline
      CAD_codingC Offline
      CAD_coding
      wrote on last edited by
      #2

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

      1 Reply Last reply
      0
      • CAD_codingC Offline
        CAD_codingC Offline
        CAD_coding
        wrote on last edited by
        #3

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

        1 Reply Last reply
        0
        • JKSHJ Offline
          JKSHJ Offline
          JKSH
          Moderators
          wrote on last edited by
          #4

          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.

          Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

          1 Reply Last reply
          0
          • CAD_codingC Offline
            CAD_codingC Offline
            CAD_coding
            wrote on last edited by
            #5

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

            1 Reply Last reply
            0
            • JKSHJ Offline
              JKSHJ Offline
              JKSH
              Moderators
              wrote on last edited by
              #6

              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.

              Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

              1 Reply Last reply
              0
              • CAD_codingC Offline
                CAD_codingC Offline
                CAD_coding
                wrote on last edited by
                #7

                ohh...
                Got it!

                1 Reply Last reply
                0
                • P Offline
                  P Offline
                  poketmonister
                  wrote on last edited by
                  #8

                  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

                  1 Reply Last reply
                  0
                  • M Offline
                    M Offline
                    Mar4eli
                    wrote on last edited by
                    #9

                    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.

                    1 Reply Last reply
                    0
                    • D Offline
                      D Offline
                      DBoosalis
                      wrote on last edited by
                      #10

                      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

                      1 Reply Last reply
                      0
                      • P Offline
                        P Offline
                        poketmonister
                        wrote on last edited by
                        #11

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

                        1 Reply Last reply
                        0
                        • W Offline
                          W Offline
                          WolfgangGriech
                          wrote on last edited by WolfgangGriech
                          #12

                          I know it's more than 10 years later, but the same issue was bugging me now at some project, and I found a really easy solution:

                          just call 'resize(0,0)' for the QDialog widget itself, after setupUi() and after deleting (my case) or adding other widgets.

                          just saw that I forgot to mention a small detail, I also set the vertical size policy of the QDialog to 'minimum', but that on its own didn't do a thing...

                          1 Reply Last reply
                          0

                          • Login

                          • Login or register to search.
                          • First post
                            Last post
                          0
                          • Categories
                          • Recent
                          • Tags
                          • Popular
                          • Users
                          • Groups
                          • Search
                          • Get Qt Extensions
                          • Unsolved