Qt: Force size of QDockWidget - c++

I have a QMainWindow with a center widget and a number of dockable widgets. The size of the various widgets get manipulated by the sliders between the dockables. I would like to fix the center widget's height to it's minimumSize height and not allow it to be made larger by the user. The width should remain relative to the QMainWindow (I only allow top and bottom dockables, so the center widget is stretched to the width of the main window).
The center widget is a QDockWidget, containing a QWidget with a QVBoxLayout (containing more stuff).
I have tried deriving from the QVBoxLayout and overriding maximumSize, but it wasn't respected. Overriding expandingDirections also didn't make any difference.
Any advise on how to achieve this?

Got it, thanks to Johannes Schaub.
I thought the QDockWidget propagates the constraints to its contained widget, which it does not. Setting maximumHeight on the inner Widget does the trick.

Related

Get size of QScrollArea viewport before showing

I have a custom QDialog comprised of a QStackedWidget with QScrollArea widgets for each page of the stacked widget.
I want to set the size hint for the QDialog such that the dialog is just large enough that the scroll bars for the scroll area are not visible when the dialog is first shown (i.e. ensure size of QScrollArea viewport = size hint of child widget in scroll area). Currently, the default sizeHint() implementation for the QDialog has insufficient height, which causes the vertical scroll bar to be shown when first loaded.
I thought this could be achieved by re-implementing sizeHint() for the QDialog, whereby the size hint of the dialog would be adjusted by the amount required for the size of QScrollArea viewport to equal the size hint for child widget in the scroll area (for the first page of the stacked layout). Unfortunately, in sizeHint(), the size of the QScrollArea viewport is set to the default size of QStackedWidget (640x480), and only updates to the correct size once the QDialog is shown.
Is there some way to get the correct size of the QScrollArea viewport before it is shown, or another way to achieve the desired effect of adjusting the size hint of the dialog to prevent scroll bars from being shown when it is first displayed (aside from hard-coding the dialog size).
With the composition of your dialog as:
I have a custom QDialog comprised of a QStackedWidget with QScrollArea
widgets for each page of the stacked widget.
The tricky part is to answer:
Is there some way to get the correct size of the QScrollArea viewport
before it is shown?
Well, before switching to certain page you can estimate the scroll area viewport if it is either correctly set or you can just measure the content going inside the scrollarea. I usually force the widget to demand certain height from the scroll area like that:
wdgetInScrollArea->setMinimumSize( widgetInScrollArea->sizeHint() );
wdgetInScrollArea->adjustSize(); // sometimes it is needed
The the scroll area viewport hint is then more 'adequate':
qDebug() << scrollArea->viewPortSizeHint(); // report
I don't see the code but usually it is not even required to do any custom event handling here, just prepare all the nested widgets like that.

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.

Customizing Qt Dock

What I'm looking to do is use the Qt Dock and the Dock widgets as they are, except I would like the show only the widget as opposed to the whole Dock.
to clarify: when a docked widget is brought into view, the whole dock appears and the widget sits on the dock. I would like to hide the portion of the dock which isn't being occupied by the widget, is there any way to do that?
Please let me know if I can clarify further.
I don't think it is feasible with Dock widgets. There are 4 fixed dock areas on a QMainWindow which are shown if a dock widget is dropped onto them.
What you might try is to disable actual docking with void setAllowedAreas(Qt::NoDockWidgetArea) and handle "snapping" yourself with void QWidget::moveEvent ( QMoveEvent * event ), snapping the widget to the main window edges if it comes within x pixels of it.
Hummm this seems difficult...
For a start, use QDockWidget::setTitleBarWidget ( QWidget * widget ) to set a custom title bar. You can create a special widget which has a small minimum size, minimum size hint, and which is not painted (or painted as invisible)...

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.

Center QGraphicsView in Widget

I have a QDialog that contains several dock widgets and one QGraphicsView. The widget layout is set to grid, the QGraphicsView size policy is set to fixed on the 2 axes and it the QGraphicsView is center in the empty zone of the QDialog.
I would like to resize my QGraphicsView and let it at the center of the empty zone of the QDialog.
I have tried this code:
// resize QGraphicsView
ui->mProjectView->resize(mProject->getSize() + QSize(2,2));
But QGraphicsView is adjusting its size to QDialog when resizing QDialog.
I'va tried then this:
// resize QGraphicsView
ui->mProjectView->resize(mProject->getSize() + QSize(2,2));
// Adjust size of QDialog to fit new widget's size
ui->centralWidget->adjustSize();
But this does not work. QGraphics View keeps last size...
I'm sure the way to achieve it is simple but I'm missing something. Can you help please?
You could try
ui->mProjectView->setFixedSize(mProject->getSize() + QSize(2,2));
instead.