QTabView hide tab content but not tabbar - c++

I have 2 QTabWidgets in a window that contain QWidgets and a additional button intended to collapse the tab content which controls visibility. Either both are visible or one is hidden.
When I set the widget visibility using the code below, the widgets disappear, but the tab widget doesn't resize.
for(int i = 0; i < tabWidget->count(); i++) {
tabWidget->widget(i)->setVisible(!hide);
}
I am left with a blank box and a frame around the edge, but nothing in the window resizes
What I would like to happen is for the tab widget that is still visible to expand to take up the whole window, but for the other tab widget's bar to still be visible.
It is possible to hide just the tab bar. Is there any way to hide the tab content and leave the bar visible?

Here are a couple of options, one of which is similar to hiding tabs in a QTabWidget.
1. Replace the tabs with dummy widgets.
You still want the tabs to show, so swap them out with an empty widget so the calculated height of the contents of the QTabWidget is 0.
2. Hide the contents of the tab's widget rather than the widget itself.
You most likely have a layout within these widgets, the layout spacing and margins will show as empty space. You would need to work out the details of hiding the contents easily.
3. Set the maximum height of the QTabWidget
Set the maximum height of the QTabWidget to the height of the QTabWidget's QTabBar.
if (hide) {
tabWidget->setMaximumHeight(tabWidget->tabBar()->height());
} else {
tabWidget->setMaximumHeight(QWIDGETSIZE_MAX); // Default maximum size for qWidgets
}
I did not see that setting QSizePolicy on either of the tab widgets had an effect. Tested on Qt 5.13.1.

Related

Auto - adjust size of icons in QToolBar after hiding a QAction button

in my GUI program I have a QToolBar at the bottom of my mainwindow which contains a number of QAction buttons for opening various dialogs.
Now, I have adjusted the size (i.e. width) of icons of the QToolBar through the QTCreator interface size policy. The size of icons are set manually in such a way that they occupy the entire width of the QToolBar.
Now, due to some required functionality of my program, I need to disable / hide one QAction button (i.e. the Icon). But when I hide that;
if(myconditions = true) //when my conditions to hide the button are met
{
ui->myaction->setVisible(false)
}
The rest of the QAction icons shift accordingly and a blank space (with the width same to the hidden icon) is left in the toolbar.
Can I adjust the size (i.e. width) of icons of the QToolBar through coding so that they occupy the entire space even if one or some gets hidden ? I couldnt find such options in the QTCreator GUI - icon size options.
From the documentation it seems it can be solved using iconSize but I dont know how to use it in code.

Add widget next to qscrollbar

How do I place a widget next to a QScrollbar like here seen:
I use a QScrollArea and overwrite the Horizontal-QScrollBar. First I thought, I could use the paintEvent to draw a text like the "100 %" next to the bar. But I can only overwrite the existing painting.
Now I think, the only opportunity would be to implement the hole QScorllBarPrivate from the source code... anyone any idea?
The main idea is to create an overlay obscuring default horizontal scroll bar and displaing your own bottom line instead.
Create a widget representing the bottom line, i.e. a widget with labels displaying some information and a horizontal QScrollBar, all together put in a hbox layout.
Put this widget in the scroll area without adding it to a layout by making QScrollArea direct parent widget of this widget.
Use move() and resize() on the bottom line widget to position it properly on initialization. Also resize and reposition it on scroll area resize (you can use event filters or inheritance to get to resize events).
Make sure that scroll area's horizontalScrollBarPolicy is Qt::ScrollBarAlwaysOn so that scroll area's internal layout always keeps space enough for bottom line widget to fit in.
Also make sure that bottom line widget has the same height as default scroll bar. It should be easy as long as you remove spacing and margins in the hbox layout and labels (or other widgets) don't require more vertical space than a scroll bar.
Use horizontalScrollBar() to get scroll area's internal QScrollBar and syncronize it with your own bar. QScrollBar has rangeChanged() and valueChanged() signals so you can connect to them and update your bar properly. When user changes value of your scroll bar and triggers its valueChanged() signal, you should set the same value for the internal scrollbar. You can protect from infinite recursion in there by using a flag that indicates that this is your own change.

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 a QLabel fill the child QWidget?

I have QWidget, used Lay Out in a Grid, then a QLabel, but there are some spaces between the QWidget and QLabel. How can I make them the same size and not break the layout?
I'm using this from UI editor.
When you select the widget, in the UI editor's property pane you should see the options for the layout that belongs to it (the options have a red background, and they're usually at the bottom of the list). Amongst the options are ones for setting the margin in pixels between widgets in the layout, if you set it to 0 it should solve your problem.

QScrollArea widget is not expanding with a Flowlayout

I have a ui with a QScrollArea Widget. The QScrollArea uses a Flowlayout. My problem is when I add widgets to my layout the scroll area begins to scroll and does not expand when it has room to expand. I want the scroll area to expand to its limit before the scroll bar appears first.
How can I get the scroll area to expand before the scroll bar appears?
can you try doing setWidgetResizable(true) for your QScrollArea
ScrollArea->setWidgetResizable(true);
A couple of suggestions:
Ensure that the size policy of the scroll area itself is Expanding.
Set the "stretch" values of the size policy of the scroll area to a value greater than that of the other widgets in the same layout. Ie:
QSizePolicy policy = pScrollArea->sizePolicy()
policy.setVerticalStretch(1);
policy.setHorizontalStretch(1);
This assumes that the siblings of the scroll area (if any) have a stretch value of 0 (the default).
Subclass the scroll area and override the sizeHint() method.