How to dock horizontally Qt widgets? - c++

I am migrating to Qt from Microsoft Visual Studio. Common design for dialog boxes in desktop applications is set of labels and fields, where dialog looks like grid with labels and related fields. So, we can have Username label and text box to the right from that label, then Password label, etc. Usually when we resize a window, label size remains fixed, and text box width is increased to fill extra space, so user has more space to enter long string in text box and see it without scrolling.
Visual Studio has docking concept to describe such layouts, so you select a control and set its resizing behavior, in our example we have to set Dock = Fill for text boxes to instruct Windows Forms how to resize them. WPF has similar functionality if you set Width = *.
However, I don't see any such properties in Qt Creator Designer.
So how to instruct Qt widget (text box for example) to fill all space available in its parent widget?

In Qt, widget geometry can be automatically managed by layouts. A widget by itself won't fill the parent. You need to set a layout on the parent widget, and add the widget to the parent. There are numerous tutorials on that.
The particular layout that would apply to your situation is QFormLayout. This answer has a complete example.

Related

Is there a way to attach or anchor two QWidgets together?

I'm getting started with Qt and decided to build a full-screen text editor. I want to have a button (button with arrow in screenshot) attached to a QDockWidget which opens and closes it so the button is always visible to the right side of the screen and stay anchored to it when dock is visible or resized.
My current app is a simple fullscreen textEdit set to centeralwidget in Mainwindow.
I haven't found a way to do this yet with layouts or existing addAnchor() functions so any help or direction is appreciated.
You can achieve what you want by using a container for your text edit and the button. A QWidget instance can be used as an "invisible"*** container for other widgets.
So in Qt Designer you add a widget as a central widget of the main-window, inside this widget you add the text edit and the button, then you set a vertical layout for this container widget.
Don't forget to restrict the docking widget to only dock to the right side, you can do that with: dock->setAllowedAreas(Qt::DockWidgetArea::RightDockWidgetArea); //assuming dock is the pointer to your QDockWidget.
In case you want the dockWidget to be able to dock to any side and the button to follow, you can do that too, but it get a little bit more complicated. Basically you need to connect a slot to dockLocationChanged of your dockWidget and based on where it's docked you need to set-up a new layout for the container widget to be vertical or horizontal and the order of the textEdit and the button based on the side the dock happened.
LE:*** you will most likely need to set the margins you want, since both the widget and it's layout can have them and the actual content might have higher spacing than you want.

Qt Ui Designer - creating window default frame and default buttons

when I create a window with the Ui Designer in QtCreator, most default templates give me a somewhat blank grey frame. There are options for adding more frames like that on the inside, but how do I add the default blue ribbon on the top with the window title, icon, minimize, maximize and close buttons? There is no option for that anywhere.
Generally, you don't - that's up to the operating system.
You can preview the window style with various skins in creator - select the skin under tools->options->designer->forms, check Print/Preview Configuration and there are various options for Style and device skins.
There are some options regarding window style under QMainWindow as well, for instance unifiedTitleAndToolBarOnMac, but it is best to leave the outer window to the OS.

Create borders around controls in MFC Form

I have an MFC form, basic stuff, a few group boxes, a few text boxes, some buttons, and a list box. What I'd like to do is add a border around all of it, preferably without a group box. Like, drawing lines along the right areas. I was told this is bad to do on a dialog though. What would I need to go about doing something like that?
I am currently using MFC C++ with Visual Studio 2008.
The easiest way is to add a Picture control to the dialog and set the style to have a border only. If the control has a width or height of 0 you can get a single line. Doing it in the dialog editor will only give you positioning down to the dialog unit, if you need pixel level control you'll have to create or reposition it in OnInitDialog.

QT: How to position a IconTool box on the right side of a window

QT 4.8, C++
I have a few dock-able icon tool box widgets in my window.
They are all displayed horizontally, on the first line.
I would like to anchor (by default) one of the tool boxes to the right corner of the window, as displayed in the pic.
Note that the the tool bar needs to keep anchored as I resize the window. Also, since the tool bars are dock-able, then can be moved or re-ordered by the user.
This is an example I made (with paint, with Visual Studio windows) that exemplify the issue:
You could probably create a QWidget and use it as a spacer in the left tool bar. Place the spacer after the undo/redo buttons and set the size policy and it's minimum width (or alternatively subclass and re-implement it's size hint) to get a desired behaviour:
QWidget spacer;
spacer.setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Fixed);
spacer.setMinimumWidth(200);
fileToolBar->addWidget(&spacer);

layout resizing in QStackedWidget Page

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.