Make QVBoxLayout scrollable using QScrollArea - c++

I have a QVBoxLayout which has content dynamically added to it. I simply want to make that layout scrollable as the content overflows. What is the correct way to achieve this in Qt Designer, or do I need code as well?
I've seen a mixed bag of examples, but none get low level enough to show exactly what needs to be done.
Thanks!

I have found a solution by doing the following.
In Qt Design view, create a QScrollArea. Then drag your existing QVBoxLayout into the QScrollArea. Right-click inside the QScrollArea (not inside the layout) and select "Apply Layout". Then choose a layout (I chose a vertical layout). That's it.
Hope this helps!

Related

Qt - How to put existing widgets and qscrollbar inside a QScrollArea

I'm a newbie in Qt.
I'm making a notepad with a ribbon:
And I wanted to make appear a QScrollBar when I resize the Window and I know that I have to use a QScrollArea for it.
My question is, how do I add existing widgets to a QScrollArea and make appear a QScrollBar when needed?
It doesn't look to me like your widgets are aligned in any form of layout. From designer, these button at the top allow you to put widgets in layouts:
Once your widgets are in their necessary layouts, make a new QScrollArea (either by doing QScrollArea *myArea = new QScrollArea() and adding it to a layout manually, or by dragging and dropping it in the designer). It should be as drag and drop as everything else you've encountered so far. You will want to set the vertical scroll policy to never scroll and set the contents margins of the scroll area's widget to 0.
But this begs the question, how savvy are you with GUI programming outside of Qt? You will run into a lot of obstacles from here on out if you can't grasp the concept of layouts and so on.
In case you don't know how layouts work, I suggest you look at the documentation. It's a good rule of thumb to check the docs and look for existing questions before asking. Manually setting the X and Y of widgets is a long-gone practice.

How to create QSplitter ui class via qt designer?

I am new to Qt and I need to implement a monitoring interface with the following considerations:
I have a main window, on which I should put multiple screens, qsplitter appears to be the best solution.
The interface provides user with the option of changing the number of cameras, so QSplitter should be created/re-created during run time.
The problem is that I have too many cameras to pre-define widgets for them, so I need to create QSplitter UI instances dynamically.
The problem is that I can't find QSplitter classes when using Qt Designer and creating QSplitter class programatically is not working as MainWindow has been created through Qt Designer (.ui).
I would like to hear any suggestions regarding this issue, if there are better approaches, please let me know.
In Qt Designer, the QSplitter is not a widget, but a Layout.
Select the widgets you want to include in the two splitter areas, then select Layout from the context menu (right mouse button) - you will find two entries Layout Horizontally in splitter and Layout Vertically in splitter to group the widgets in a vertical or in a horizontal splitter.
QSplitter isn't a strict UI element, it's essentially a parent element that controls child elements. If you want to do it through Designer you'd probably run into headaches, but the basic gist is you select a number of widgets to be controlled by it, and click the Layout Horizontally/Vertically in splitter button which is in the layout buttons group.
What you might be best doing is creating your child elements programmatically, creating your splitter programmatically, adding the child widgets with someSplitter->addWidget(...). In the Qt docs there's some sample code for this:
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);
http://doc.qt.io/qt-5/qsplitter.html#details
And if you really want to do it in Designer there's a guide here: http://www.bogotobogo.com/Qt/Qt5_Splitter.php
Although QSplitter is a widget, you can't create one directly in Qt Designer. It is only available for laying out pre-existing widgets - which does not fit your use-case, since you need to create child widgets dynamically.
However, you can work around this limitation by using widget promotion. This is a simple mechanism that allows you to add substitute classes to represent widgets that are not directly available in Qt Designer. The idea is that you add a widget that is most similar to the one you actually want (e.g. a QFrame is most similar to QSplitter) and then promote that to the substitute class which you have defined in your own header file. When uic finally generates the code, it will use your substitute class instead of the class of the widget added in Qt Designer (which just acts as a placeholder).
Note that when you create your substitute class in the Promoted Widgets dialog, the base class should be a QFrame, not a QSplitter. This is because you are extending a QFrame (i.e. your placeholder widget), rather than a QSplitter. Of course, you can define your substitute class to be anything you like.
Image is easier to understand.

How to activate centralWidget in QT Designer

I was looking at this article
How to make a Qt Widget grow with the window size?
but when i got to the answer I got stuck on "activating" the central widget. I notice an icon with a red circle so I guess that means its disabled. I've been searching the net to try to figure out how to "activate" it but I am not having any luck.
Can someone please help me out?
Have a look at the layout system.
That icon does not mean your QWidget is disabled, that just mean you do not apply a layout on it.
Try to press like Ctrl+1 in order to apply a basic layout. If nothing has changed, you might need to put a QWidget inside the central widget first and then apply the layout.

fastest and best way to add scrollbar to a widget in qt

I have a widget implemented with Qt5.0 but it is too large and does not have any scroll bars? how can I add it with minimum lines of codes?
If you don't want to put your widget in a QScrollArea, you can make your widget a descendent of QScrollArea itself. All of the Qt item views do this (QListView etc).

MFC equivalent of Qt QVBoxLayout widget

I'd like to know how to position MFC controls in the dialog so that they stretch to fill free space in the dialog. In Qt it is possible to nest QVBoxLayout and QHBoxLayout widgets, I'm looking for something similar in MFC.
Additionally I'd be interested to know if there exists an MFC control similar to the Qt QTableWidget that can have other widgets nested in each cell.
Not difficult to find on the web..
Resizable layouts:
http://www.codeproject.com/KB/dialog/WndResizer.aspx
Not sure on the grid control with widgets in it, maybe this will do it with some modifications:
http://www.codeproject.com/KB/miscctrl/gridctrl.aspx
As far as I know, there is no layout managers in MFC. A big lack indeed...