QTreeView child of QMainWindow not resizing with main window
-
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):
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?
-
@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 theElementView
. Drag a newElementView
straight on thecentralWidget
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 withmainWindow
?@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.
IfElementView
is aQTableView
in your*.ui
file, you can simply access it withui->ElementView
anywhere in your
mainWindow
class -
@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 yourresizeEvent
-
@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?
-
Rightclick it and select layout in context menu.
-
@Pl45m4 said in QTreeView child of QMainWindow not resizing with main window:
Rightclick it and select layout in context menu.
That was my first hunch, but this is the context menu when I rightclick the centralwidget:
No layout in the menu, so I tried rightclicking the horizontalLayout:
This menu had a Lay out choice, but its submenu had choices that did not seem to help. -
@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 yourElementView
, so that only yourcentralWidget
is left. Then drag theElementView
in again, that it is in the direct child branch ofcentralWidget
. Then right-click anywhere on the empty space of the widget itself or rightclickcentralWidget
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 themainWindow
(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 aQHBoxLayout
, 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. :-) -
@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.
-
@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 theElementView
. Drag a newElementView
straight on thecentralWidget
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 withmainWindow
?@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.
IfElementView
is aQTableView
in your*.ui
file, you can simply access it withui->ElementView
anywhere in your
mainWindow
class -
@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! -
@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().
-