layout resizing in QStackedWidget Page - c++

I'm trying to get a widget placed on a page in a QStackedWidget to resize automatically when the parent QStackedWidget resizes. This simple task seems impossible to do with Qt Creator. Adding a layout to the page does not seem to do anything, the layout just stays at whatever size it's designed as, and will not resize. This is (I think?) because the layout is being added into the page widget as a child and not directly attached to the page widget. There seems to be no way to actually add a layout to page.
How do a get a widget to resize in a QStackedWidget with Qt Creator???
As a side comment, I really wish Qt would abandon it's layout system, it is far far harder to use then an anchoring based system.

Qt Creator treats widgets as if they already have a layout on them which you can access from the context menu (right-click) of the widget in the designer or from the widget list at the right. This means that you only have to drop layouts (from the palette) on a widget if you want to divide that top level layout into further sub layouts. The image below shows this context menu.
Notice the widget icons on the right that show a crossed-out red circle. This indicates that no top-level layout has been defined for those widgets. Once a layout type has been chosen, the red circle will disappear and the icon will reflect your layout choice.
In your case, after you drop a QStackedWidget on your main widget, you can right-click the main widget, select "Lay out" and choose "Lay Out Horizontally". This will make the stacked widget fill the entire main widget and resize with it.
Likewise, if you drop any widgets on one of the "page" widgets, you can then right-click the page widget and assign a top level layout for that which will cause those widgets to resize accordingly with the page widget.

Related

Qt dock neighbour widget behaviour

I have two Qt widgets in the window. One of them is QDockWidget, and another one is just QWidget.
When I drag the QDockWidget, the default behaviour of the another widget is moving without changing its size. And I want it to fill the whole window except dock widget, and to change its size programmatically when I drag QDockWidget. Hwo to do it better?
The solution was to set container horizontal policy as "fixed"

How to manage QSplitter in Qt Designer

When I press a button, I bring up a dialog where user select things and press 'Ok' at the end. I want a splitter in this dialog. Left pane will show tree and right will show something else. How do I do that right?
From Qt example itself:
QSplitter *splitter = new QSplitter(parent);
QListView *listview = new QListView;
QTreeView *treeview = new QTreeView;
QTextEdit *textedit = new QTextEdit;
splitter->addWidget(listview);
splitter->addWidget(treeview);
splitter->addWidget(textedit);
So in this example, splitter is created without any dialog resource. If I have to create this way, that would mean I have to create all my controls in the code as well rather than Qt Creator.
What is the right way to do this when I need other controls on the screen?
You can simply create splitter containing items in Qt Designer :
First place your widgets on your dialog or widget in designer (They should not be in a layout)
Select the widgets that you want to be in a splitter (By holding CTL and clicking on them)
Right click on a selected widget and from Layout menu select Lay Out Horizontally in Splitter or Lay Out Vertically in Splitter.
Now apply a grid layout to the dialog and everything should be OK. You would see something like this in Object Inspector View :
Okay, I know this is ancient, but here's the complete answer.
First, within some sort of widget container, plop your pieces in. For the window I just did, I have a Widget as my window. I put two widgets inside that labeled something like topContainer and bottomContainer. I then put all the widgets they each need into them, and gave them their own layouts.
Then do NOT select the main container. Select the two widgets you want to split. You're in effect putting a splitter on them, not on the main container. So I went to the widget list window and selected both together, then right-click for the dialog window, scroll down to the Layout option, and "Lay Out Vertically in a Splitter" is NOT greyed out. Select it.
You still need a layout on the main container. A splitter is not a layout. So at that point, I just put a vertical layout on the main container.
To repeat: you are NOT setting a layout on the container holding the pieces you're trying to split. You are selecting the two widgets to split and adding a QSplitter around them. That's the trick to get it to work.
You can still create your controls in a .ui file using Qt Designer (integrated in Qt Creator). Within Qt Designer, add a QWidget object to your dialog. Then, from QDialog derived class you'll write, directly in your constructor, create your QSplitter using the QWidget object as a parent.
This way, you can create all but the splitter object from Qt Designer.
I think it's also possible to create the QSplitter (as you can create a QButton, QCheckBox...) item directly from Qt Designer.

Make a Toolbar have a grid layout

I want to make a QToolBar have 3 columns of buttons when docked on the left side of the QMainWindow, but have 1 row when docked on the top of the main window. Is this possible?
I have a tried using a QToolBar with a custom layout, but the normal re-size behavior of the QToolBar doesn’t work (doesn’t hide widgets behind an expand button when its too small). The non-working expand button isn’t that big of a deal, but the bigger problem is that the custom layout prevents the main window from being smaller than the toolbar.
I was able to get my desired behavior by putting each row of Tool Buttons in a QHBoxLayout, putting that layout in a empty QWidget, and calling toolBar->addWidget( widget ) for each row. This gives me a grid toolbar when the toolbar is mounted on the left, and single horizontal bar when mounted on the top.

How to make the QTabWidget expandable with the form

In QT Designer, I have a form into which I have added a QTabWidget.
But during preview, when I expand the form, the tab stays as it is.
How can I make the tab expand.
Is this a limitation of the QT designer and when i actually run it will the tab expand?
I have set the size policy as expandable for both x, y.
Is there anything else I should be doing to get this?
You should add layout to the widget. Only if a widget has a layout, its contents will be moved and resized automatically when widget is resized.
To do this, you need to right-click on main widget in Designer and choose "Add layout -> Grid layout" (for example).

Adding scroll bar to widget containing a layout in QT C++

I am new to QT and I am creating a widget that has a gridlayout. The gridlayout contains a matrix of QLineEdit widgets. The window resizes to fit the layout but when layout is large it goes off screen. When I maximize the screen, the QLineEdit widgets are resized to fit the screen and for large layouts they become extremely small.
I want to be able to resize the window without resizing the QLineEdit widgets and add scroll bars to navigate.
I tried the following with no luck:
Window->resize(QSize(500,500));
QScrollArea *scrollArea = new QScrollArea;
scrollArea->setWidget(Window);
where window is the widget containing the layout. Also, the window closes when after executing "scrollArea->setWidget(Window);" and I dont why.
If someone can help me out I would really appreciate it.
Thank You!
For disabling the vertical resize on the widgets, why don't you just use the setFixedHeight() method on the widgets?
For the menu bar, why don't you take it out of the widget that is scrollable. You can have a layout for the window that contains the menu bar and then the widget that contains everything else (scrollable part). Is that what you are looking for?
I fixed my problem by creating a QMainWindow with the menu bar. Then created a widget which includes the layout, set the Scroll Area to the widget. Finally set the central widget of the main widow to the scroll area.