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. QTreeView child of QMainWindow not resizing with main window

QTreeView child of QMainWindow not resizing with main window

Scheduled Pinned Locked Moved Solved General and Desktop
qmainwindowqtableviewresizeevent
10 Posts 3 Posters 1.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.
  • C Offline
    C Offline
    Calvin H-C
    wrote on last edited by
    #1

    I am using a QTreeView that is supposed to make use of the entire client area of my main window (derived from QMainWindow). This is defined in my MainWindow.ui file as this (from Design mode - ElementView is the name of the QTreeView):
    Objects.png

    I couldn't find a way to have the QTreeView automatically track the size of the main window, so I figured I would have to do this in the main window's resizeEvent() function:

    void MainWindow::resizeEvent( QResizeEvent * event )
    {
    	ui->centralwidget->resize( event->size().width(), event->size().height() );
    	List->resize( event->size().width(), event->size().height() - statusBar()->size().height() );
    	QMainWindow::resizeEvent( event );
    }
    

    I have tried only calling resize for the centralwidget, as well as only for List. By the way, List is initialized in the constructor of my MainWindow class as:

    	List = ui->centralwidget->findChild<QTableView*>( "ElementView" );
    

    What am I missing? How to I get the QTreeView to make use of the full space available in my main window?

    Pl45m4P 1 Reply Last reply
    0
    • C Calvin H-C

      @Pl45m4 said in QTreeView child of QMainWindow not resizing with main window:

      The steps above should fix it.

      Initially, it appeared they did. Though, I was unable to do the "click 'Lay out' and pick the layout you want as toplevel layout", as everything except 'Adjust size' was greyed out. I'm thinking I might need to edit the UI file manually.

      Things appeared to be working fine initially, but then I removed my resizeEvent and it stopped working. I've been trying to find why the layout choices are greyed out before posting this response, but didn't figure it out.

      I did find that leaving my resizeEvent only required setting the size of the QTableView, and setting its width to the value passed in, but setting its height to the value passed in less the sum of the heights of the menubar, toolbar, and statusbar:

      	List->resize( event->size().width(), event->size().height() -
      				  ( statusBar()->size().height() + menuBar()->size().height() + ui->toolBar->size().height() ) );
      

      I usually prefer to learn things the "hard" way before moving to a simpler interface to know when one is better than the other. In this case, I'm trying to learn QT while trying to unlearn over three decades of using MFC.

      Pl45m4P Offline
      Pl45m4P Offline
      Pl45m4
      wrote on last edited by Pl45m4
      #8

      @Calvin-H-C said in QTreeView child of QMainWindow not resizing with main window:

      I was unable to do the "click 'Lay out' and pick the layout you want as toplevel layout", as everything except 'Adjust size' was greyed out. I'm thinking I might need to edit the UI file manually.

      You need some content first. For some reason you can't assign a layout to an empty container/widget.
      Remove the current layout and the ElementView. Drag a new ElementView straight on the centralWidget and then rightclick and "lay out".
      There is no way that this doesn't work.

      List->resize( event->size().width(), event->size().height() -
      ( statusBar()->size().height() + menuBar()->size().height() + ui->toolBar->size().height() ) );

      I still don't get the purpose of this...Is it because your ElementView is not resizing with mainWindow?

      @Calvin-H-C said in QTreeView child of QMainWindow not resizing with main window:

      List = ui->centralwidget->findChild<QTableView*>( "ElementView" );

      Btw: AFAICS this is also not needed.
      If ElementView is a QTableView in your *.ui file, you can simply access it with

      ui->ElementView
      

      anywhere in your mainWindow class


      If debugging is the process of removing software bugs, then programming must be the process of putting them in.

      ~E. W. Dijkstra

      JonBJ C 2 Replies Last reply
      0
      • C Calvin H-C

        I am using a QTreeView that is supposed to make use of the entire client area of my main window (derived from QMainWindow). This is defined in my MainWindow.ui file as this (from Design mode - ElementView is the name of the QTreeView):
        Objects.png

        I couldn't find a way to have the QTreeView automatically track the size of the main window, so I figured I would have to do this in the main window's resizeEvent() function:

        void MainWindow::resizeEvent( QResizeEvent * event )
        {
        	ui->centralwidget->resize( event->size().width(), event->size().height() );
        	List->resize( event->size().width(), event->size().height() - statusBar()->size().height() );
        	QMainWindow::resizeEvent( event );
        }
        

        I have tried only calling resize for the centralwidget, as well as only for List. By the way, List is initialized in the constructor of my MainWindow class as:

        	List = ui->centralwidget->findChild<QTableView*>( "ElementView" );
        

        What am I missing? How to I get the QTreeView to make use of the full space available in my main window?

        Pl45m4P Offline
        Pl45m4P Offline
        Pl45m4
        wrote on last edited by Pl45m4
        #2

        @Calvin-H-C said in QTreeView child of QMainWindow not resizing with main window:

        What am I missing?

        Your central widget is missing something... a layout ;-)
        Assign the horizontal layout to your centralwidget, put the ElementView in and it should work.

        Edit:
        You see the red warning sign. It means, that this container/widget has no layout.
        Btw. just dragging other layouts inside the area isn't enough. You need to actually set it. Then you can delete what's in your resizeEvent


        If debugging is the process of removing software bugs, then programming must be the process of putting them in.

        ~E. W. Dijkstra

        C 1 Reply Last reply
        3
        • Pl45m4P Pl45m4

          @Calvin-H-C said in QTreeView child of QMainWindow not resizing with main window:

          What am I missing?

          Your central widget is missing something... a layout ;-)
          Assign the horizontal layout to your centralwidget, put the ElementView in and it should work.

          Edit:
          You see the red warning sign. It means, that this container/widget has no layout.
          Btw. just dragging other layouts inside the area isn't enough. You need to actually set it. Then you can delete what's in your resizeEvent

          C Offline
          C Offline
          Calvin H-C
          wrote on last edited by Calvin H-C
          #3

          @Pl45m4 said in QTreeView child of QMainWindow not resizing with main window:

          @Calvin-H-C said in QTreeView child of QMainWindow not resizing with main window:

          What am I missing?

          Your central widget is missing something... a layout ;-)
          Assign the horizontal layout to your centralwidget, put the ElementView in and it should work.

          Silly question: how do I assign the horizontal layout to my centralwidget?

          Pl45m4P 1 Reply Last reply
          0
          • C Calvin H-C

            @Pl45m4 said in QTreeView child of QMainWindow not resizing with main window:

            @Calvin-H-C said in QTreeView child of QMainWindow not resizing with main window:

            What am I missing?

            Your central widget is missing something... a layout ;-)
            Assign the horizontal layout to your centralwidget, put the ElementView in and it should work.

            Silly question: how do I assign the horizontal layout to my centralwidget?

            Pl45m4P Offline
            Pl45m4P Offline
            Pl45m4
            wrote on last edited by Pl45m4
            #4

            @Calvin-H-C

            Rightclick it and select layout in context menu.


            If debugging is the process of removing software bugs, then programming must be the process of putting them in.

            ~E. W. Dijkstra

            C 1 Reply Last reply
            0
            • Pl45m4P Pl45m4

              @Calvin-H-C

              Rightclick it and select layout in context menu.

              C Offline
              C Offline
              Calvin H-C
              wrote on last edited by
              #5

              @Pl45m4 said in QTreeView child of QMainWindow not resizing with main window:

              @Calvin-H-C

              Rightclick it and select layout in context menu.

              That was my first hunch, but this is the context menu when I rightclick the centralwidget:
              Menu1.png
              No layout in the menu, so I tried rightclicking the horizontalLayout:
              Menu2.png
              This menu had a Lay out choice, but its submenu had choices that did not seem to help.

              Pl45m4P 1 Reply Last reply
              0
              • C Calvin H-C

                @Pl45m4 said in QTreeView child of QMainWindow not resizing with main window:

                @Calvin-H-C

                Rightclick it and select layout in context menu.

                That was my first hunch, but this is the context menu when I rightclick the centralwidget:
                Menu1.png
                No layout in the menu, so I tried rightclicking the horizontalLayout:
                Menu2.png
                This menu had a Lay out choice, but its submenu had choices that did not seem to help.

                Pl45m4P Offline
                Pl45m4P Offline
                Pl45m4
                wrote on last edited by Pl45m4
                #6

                @Calvin-H-C said in QTreeView child of QMainWindow not resizing with main window:

                No layout in the menu, so I tried rightclicking the horizontalLayout:

                Maybe you figured it out already... actually I'm using QtDesigner not that much, for a reason, so maybe I didn't picked the right steps, but anyway.
                What definitely works:
                Remove your additional horizontal layout and your ElementView, so that only your centralWidget is left. Then drag the ElementView in again, that it is in the direct child branch of centralWidget. Then right-click anywhere on the empty space of the widget itself or rightclick centralWidget in your ObjectTree again, click "Lay out" and pick the layout you want as toplevel layout (it says something like "Lay out horizontally")

                This menu had a Lay out choice, but its submenu had choices that did not seem to help.

                What you currently have and what also the cause of your issue, your ElementView is in a horizontal layout, but this layout is not connected to the mainWindow (i.e. centralWidget). It's loose and floating around and therefore not adjusting with your window size.
                The steps above should fix it.

                Note:
                When you work with QtDesigner you only need to add cascasding layouts yourself. Every other container/widget comes with a "lay out" option, where you can throw stuff in, pick the right layout and it's added automatically.
                "Behind the scenes" there will be still a QHBoxLayout, but that's another thing, as you are working with a WYSIWYG editor/designer and that's one of its purposes - not to worry how the code for your UI looks like, otherwise you would write it yourself ;-)
                After all, QtDesigner can be good to create simple UIs in a short amount of time, but sooner or later you will reach its limits and get to a point where you have to write/align the widgets by code.
                Then you can mix both ways or forget about the *.ui files and write your GUI yourself from the beginning. :-)


                If debugging is the process of removing software bugs, then programming must be the process of putting them in.

                ~E. W. Dijkstra

                C 1 Reply Last reply
                0
                • Pl45m4P Pl45m4

                  @Calvin-H-C said in QTreeView child of QMainWindow not resizing with main window:

                  No layout in the menu, so I tried rightclicking the horizontalLayout:

                  Maybe you figured it out already... actually I'm using QtDesigner not that much, for a reason, so maybe I didn't picked the right steps, but anyway.
                  What definitely works:
                  Remove your additional horizontal layout and your ElementView, so that only your centralWidget is left. Then drag the ElementView in again, that it is in the direct child branch of centralWidget. Then right-click anywhere on the empty space of the widget itself or rightclick centralWidget in your ObjectTree again, click "Lay out" and pick the layout you want as toplevel layout (it says something like "Lay out horizontally")

                  This menu had a Lay out choice, but its submenu had choices that did not seem to help.

                  What you currently have and what also the cause of your issue, your ElementView is in a horizontal layout, but this layout is not connected to the mainWindow (i.e. centralWidget). It's loose and floating around and therefore not adjusting with your window size.
                  The steps above should fix it.

                  Note:
                  When you work with QtDesigner you only need to add cascasding layouts yourself. Every other container/widget comes with a "lay out" option, where you can throw stuff in, pick the right layout and it's added automatically.
                  "Behind the scenes" there will be still a QHBoxLayout, but that's another thing, as you are working with a WYSIWYG editor/designer and that's one of its purposes - not to worry how the code for your UI looks like, otherwise you would write it yourself ;-)
                  After all, QtDesigner can be good to create simple UIs in a short amount of time, but sooner or later you will reach its limits and get to a point where you have to write/align the widgets by code.
                  Then you can mix both ways or forget about the *.ui files and write your GUI yourself from the beginning. :-)

                  C Offline
                  C Offline
                  Calvin H-C
                  wrote on last edited by
                  #7

                  @Pl45m4 said in QTreeView child of QMainWindow not resizing with main window:

                  The steps above should fix it.

                  Initially, it appeared they did. Though, I was unable to do the "click 'Lay out' and pick the layout you want as toplevel layout", as everything except 'Adjust size' was greyed out. I'm thinking I might need to edit the UI file manually.

                  Things appeared to be working fine initially, but then I removed my resizeEvent and it stopped working. I've been trying to find why the layout choices are greyed out before posting this response, but didn't figure it out.

                  I did find that leaving my resizeEvent only required setting the size of the QTableView, and setting its width to the value passed in, but setting its height to the value passed in less the sum of the heights of the menubar, toolbar, and statusbar:

                  	List->resize( event->size().width(), event->size().height() -
                  				  ( statusBar()->size().height() + menuBar()->size().height() + ui->toolBar->size().height() ) );
                  

                  I usually prefer to learn things the "hard" way before moving to a simpler interface to know when one is better than the other. In this case, I'm trying to learn QT while trying to unlearn over three decades of using MFC.

                  Pl45m4P 1 Reply Last reply
                  0
                  • C Calvin H-C

                    @Pl45m4 said in QTreeView child of QMainWindow not resizing with main window:

                    The steps above should fix it.

                    Initially, it appeared they did. Though, I was unable to do the "click 'Lay out' and pick the layout you want as toplevel layout", as everything except 'Adjust size' was greyed out. I'm thinking I might need to edit the UI file manually.

                    Things appeared to be working fine initially, but then I removed my resizeEvent and it stopped working. I've been trying to find why the layout choices are greyed out before posting this response, but didn't figure it out.

                    I did find that leaving my resizeEvent only required setting the size of the QTableView, and setting its width to the value passed in, but setting its height to the value passed in less the sum of the heights of the menubar, toolbar, and statusbar:

                    	List->resize( event->size().width(), event->size().height() -
                    				  ( statusBar()->size().height() + menuBar()->size().height() + ui->toolBar->size().height() ) );
                    

                    I usually prefer to learn things the "hard" way before moving to a simpler interface to know when one is better than the other. In this case, I'm trying to learn QT while trying to unlearn over three decades of using MFC.

                    Pl45m4P Offline
                    Pl45m4P Offline
                    Pl45m4
                    wrote on last edited by Pl45m4
                    #8

                    @Calvin-H-C said in QTreeView child of QMainWindow not resizing with main window:

                    I was unable to do the "click 'Lay out' and pick the layout you want as toplevel layout", as everything except 'Adjust size' was greyed out. I'm thinking I might need to edit the UI file manually.

                    You need some content first. For some reason you can't assign a layout to an empty container/widget.
                    Remove the current layout and the ElementView. Drag a new ElementView straight on the centralWidget and then rightclick and "lay out".
                    There is no way that this doesn't work.

                    List->resize( event->size().width(), event->size().height() -
                    ( statusBar()->size().height() + menuBar()->size().height() + ui->toolBar->size().height() ) );

                    I still don't get the purpose of this...Is it because your ElementView is not resizing with mainWindow?

                    @Calvin-H-C said in QTreeView child of QMainWindow not resizing with main window:

                    List = ui->centralwidget->findChild<QTableView*>( "ElementView" );

                    Btw: AFAICS this is also not needed.
                    If ElementView is a QTableView in your *.ui file, you can simply access it with

                    ui->ElementView
                    

                    anywhere in your mainWindow class


                    If debugging is the process of removing software bugs, then programming must be the process of putting them in.

                    ~E. W. Dijkstra

                    JonBJ C 2 Replies Last reply
                    0
                    • Pl45m4P Pl45m4

                      @Calvin-H-C said in QTreeView child of QMainWindow not resizing with main window:

                      I was unable to do the "click 'Lay out' and pick the layout you want as toplevel layout", as everything except 'Adjust size' was greyed out. I'm thinking I might need to edit the UI file manually.

                      You need some content first. For some reason you can't assign a layout to an empty container/widget.
                      Remove the current layout and the ElementView. Drag a new ElementView straight on the centralWidget and then rightclick and "lay out".
                      There is no way that this doesn't work.

                      List->resize( event->size().width(), event->size().height() -
                      ( statusBar()->size().height() + menuBar()->size().height() + ui->toolBar->size().height() ) );

                      I still don't get the purpose of this...Is it because your ElementView is not resizing with mainWindow?

                      @Calvin-H-C said in QTreeView child of QMainWindow not resizing with main window:

                      List = ui->centralwidget->findChild<QTableView*>( "ElementView" );

                      Btw: AFAICS this is also not needed.
                      If ElementView is a QTableView in your *.ui file, you can simply access it with

                      ui->ElementView
                      

                      anywhere in your mainWindow class

                      JonBJ Offline
                      JonBJ Offline
                      JonB
                      wrote on last edited by
                      #9

                      @Pl45m4 said in QTreeView child of QMainWindow not resizing with main window:

                      You need some content first. For some reason you can't assign a layout to an empty container/widget.

                      @Calvin-H-C
                      This from @Pl45m4. In an unintelligible and highly beginner-unfriendly behaviour, Qt Designer will not allow you to assign a layout to a widget until after you have first added a child widget onto it. At which point the Layout menu items become enabled. You can even delete any child widgets after you have been able to assign a layout to the parent this way and the layout will remain. You can assign a layout from code without requiring any child widgets, so goodness knows why Designer enforces this. There is no reason you should not be able to design a widget with some layout yet no children, but Designer makes this hard!

                      1 Reply Last reply
                      1
                      • Pl45m4P Pl45m4

                        @Calvin-H-C said in QTreeView child of QMainWindow not resizing with main window:

                        I was unable to do the "click 'Lay out' and pick the layout you want as toplevel layout", as everything except 'Adjust size' was greyed out. I'm thinking I might need to edit the UI file manually.

                        You need some content first. For some reason you can't assign a layout to an empty container/widget.
                        Remove the current layout and the ElementView. Drag a new ElementView straight on the centralWidget and then rightclick and "lay out".
                        There is no way that this doesn't work.

                        List->resize( event->size().width(), event->size().height() -
                        ( statusBar()->size().height() + menuBar()->size().height() + ui->toolBar->size().height() ) );

                        I still don't get the purpose of this...Is it because your ElementView is not resizing with mainWindow?

                        @Calvin-H-C said in QTreeView child of QMainWindow not resizing with main window:

                        List = ui->centralwidget->findChild<QTableView*>( "ElementView" );

                        Btw: AFAICS this is also not needed.
                        If ElementView is a QTableView in your *.ui file, you can simply access it with

                        ui->ElementView
                        

                        anywhere in your mainWindow class

                        C Offline
                        C Offline
                        Calvin H-C
                        wrote on last edited by
                        #10

                        @Pl45m4 said in QTreeView child of QMainWindow not resizing with main window:

                        Remove the current layout and the ElementView. Drag a new ElementView straight on the centralWidget and then rightclick and "lay out".

                        I was doing this, but I misinterpreted what the rightclick applied to. I thought it applied to the new Element View I dragged onto the centralWidget, and its "Lay out" menu had most everything disabled. When I tried rightclicking on the centralWidget outside of the ElementView, its menu had the selections all available.

                        All is working now and I have removed my resizeEvent().

                        @Pl45m4 and @JonB - All comments are much appreciated!

                        1 Reply Last reply
                        1
                        • C Calvin H-C has marked this topic as solved on

                        • Login

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