Make QListWidget fill QFormLayout - qt

I am using QT4 Designer have a layout that looks basically like this:
QWidget
QVBoxLayout
QFormLayout
QLineEdit
QListWidget
QPushButton
Each control is on a separate line.
I did the layout assignment trick to get everything to fit to the parent, and if I reduce the height the QListWidget shrinks as hoped, but if I increase the height the QWidget.geometry.Height never exceeds 192. The Vertical Policy is set to Expanding and the maximumSize.Height is 16777215, but no other policy seems to allow the QListWidget to grow. Width grows correctly, however.
What setting am I missing? Should I instead anchor the QPushButton to the bottom of the QFormLayout, and if so, how?

Well, I found my own answer - I selected the QFormLayout, right-clicked, and chose Lay out/Lay Out in a Grid and everything snapped into place! So, the new, working hierarchy is:
QWidget
QVBoxLayout
QGridLayout <<-- Changed from QFormLayout
QLineEdit
QListWidget
QPushButton

Related

when is the size of a QWidget in a QGridLayout specified?

in my Qt-application I add a QGridLayout to the main window. Afterwards, i include some QLabels, some with QSizePolicy::Minimum, others with QSizePolicy::Expanding.
I add the labels into the grid in the resizeEvent of the window.
At the end of the resizeEvent-method, I put out the height of one label.
surprisingly, i always get the height of the whole window, never mind, which label's height i want to know.
when the resizeEvent is called for the second time, i get the label's height, which is much smaller than the windowHeight.
The Labels are definitly initialized before in the constructor of the mainWindow. There, I also set the central Layout to the qGridLayout.
Can anybody tell me, why I'm getting such awesome values for the label's height?
Thank you in advance
Robby

Adding QWidget without layout to QStackedWidget inlay gap

I have a QStackedWidget that I add multiple QWidget() objects to. Each QWidget has its own layout. For some reason when it adds the QWidget to the QStacked widget, it doesn't add it to say location "0,0"... it seems to always inlay it by 3 or 4 pixels from each edge of the QStackedWidget. See picture for an example...I made the QStackedWidget background black so it is easy to see the gap.
The question is, how can I eliminate this gap and get the QWidget to align perfectly with the QStackedWidget?
If I use the "setContentsMargins(0,0,0,0) on the layout I use for the QWidget the gap goes away.
That is the solution.

Qt ScrollArea on widget messes up size and position of widget [Qt 5.1]

I'm new with Qt and I want to implement a scrollable widget which can grow dynamically, e.g. by adding buttons into it when another button is pressed. I try to implement it using the following layout and code:
scrollArea = new QScrollArea(ui->outerWidget);
scrollArea->setWidget(ui->innerWidget);
layout = new QVBoxLayout(ui->outerWidget);
ui->innerWidget->setLayout(layout);
scrollArea->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
scrollArea->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
// code for PushButton to add buttons in innerWidget
void MainWindow::on_pushButton_clicked()
{
QPushButton *button = new QPushButton("button"+QString::number( nameCounter ));
nameCounter ++;
ui->innerWidget->layout()->addWidget(button);
}
This implementation has two problems when I run the program, first the innerWidget appears out of place (I define its position in Qt Creator's Design mode) and second after many widgets are added in the layout the scroll bar is doesn't grow, but instead the widgets are become smaller to fit into the layout:
In another thread it was suggested to set widgetResizable to true:
scrollArea->setWidgetResizable(true);
The scroll bar seems to work now but the new problem is that the innerWidget becomes very small so it is barely visible:
So, how can I achieve a scrollable widget that will respect the size and position of the design?
have no environment to verify but good luck.
to place scrollArea the target position,
Manually call SetGeometry, or
Place a QScrollArea in ui form, (suggested if it's static), or
Place a widget in your target position, and set QScrollArea's parent to it, and add QScrollArea to its layout, and Set QScrollArea to be expanding.
manually create a QWidget innerWidget and assign it to scrollArea via QScrollArea::setWidget(*QWidget), try different size policy to innerWidget, such as "Preferred".
Also be aware of Qt's reference mentioned : void QScrollArea::setWidget ( QWidget * widget )
Sets the scroll area's widget.
....
Note that You must add the layout of widget before you call this function; if you add it later, the widget will not be visible - regardless of when you show() the scroll area. In this case, you can also not show() the widget later.
Above list solutions, below are reasons to the problems you mentioned:
scrollArea->setWidget(ui->innerWidget); Setting a widget to ScrollArea will change the parenting and layouting of ui->innerWidget, so the geometry values written in ui form (in Qt Creater) will no longer take effect, this is the reason of innerWidget out of place. ui->innerWidget is no longer a child of outerWidget, it's geometry will follow its new parent (but not scrollArea, there's some tricky layouting inside QScrollArea". To be clear, innerWidget is not helpful to locate scrollArea in such scenario.
In your first clip of code, widget 'scrollArea' is created with parent outerWidget, again no size policy or layout or geometry is specified, so scrollArea will by default be placed at the left top corner of the parent "outerWidget". To place scrollArea to your target geometry, you can " set geometry manually " or " assign innerWidget as scrollArea's parent and expand scrollArea". Obviously the latter method cannot assign ui->innerWiget to scrollArea->setWidget().
scrollArea->setWidgetResizable(true); makes the scrollArea "shrink" at left top corner of outerWidget. This is because, QScrollArea does not increase along with it's contents, it can scroll to display all of it contents so the required size of QScrollArea can be as small as possible. Once the 'Resizable' property is set to "true", QScrollArea decides to shrink to its minimum necessary size, thus the size to display its scroll bar and scroll buttons....

How to prevent QTableWidget from occupying the whole window in a QHBoxLayout?

In my Qt program, I programmatically generate a modal QDialog. I want to show two widgets in this dialog window: A custom widget showing a camera output and a QTableWidget, showing the pixel coordinates of the corners found in the camera image. I generate a QHBoxLayout and add my custom widget and the QTableWidget into it. Then I set this QHBoxLayout as the Layout of the QDialog window. What I want to achieve is to share the available space in the QDialog's window area equally between my custom QWidget and the QTableWidget, horizontally, by using a QHBoxLayout. But I always end up with QTableWidget occupying the whole QDialog area, by overlapping my custom widget. How can I instruct these two widgets to exactly share the QDialog area?? Note that I first add my custom widget and then the QTableWidget into the QHBoxLayout.
Make sure on your custom widget you've specified a minimumSizeHint and a sizeHint, this instructs the QLayout manager that the widget requires a specific space. To have them split equally you'll be best off detecting the size of the QDialog and then specifying the width for both by removing the boundary sizes (spacing between widgets + space to QDialog edge) and dividing it up.

Resizing a QDialog after adding components to a child widget

I'm a bit new to QT but have to work on existing code. Here's the case:
I have a class extending QDialog. the constructor sets a QGridLayout then adding three other widgets to it. One of the widgets is a QScrollArea containing a QGroupBox. this QGroupBox has a QVBoxLayout and there I'm adding a list of widgets at runtime. The size of the scroll area should grow until a given limit is reached before showing the scrollbars so that they are only used when the dialog would grow too high. I've found that the sizeHint of the outer layout doesn't update when the sizeHint of the scroll area updates. How can I refresh this, or is there a better way to resize the parent dialog?
What about using widgetResizable property of QScrollArea? It should try to resize view to avoid using scorllbars.

Resources