Finding the maximum width of a column in QGridLayout - qt

I have a QGridLayout and a QScrollArea inside one of the columns. Also, I have a QGroupBox inside the QScrollArea, where I list a number of combo boxes. Basically, what I want to do is avoid having horizontal scroll bars in the QScrollArea, and only have the vertical bars if the number of combo boxes is large. This can be done by fixing the width of the QGroupBox.
However, I don't have the size hardcoded, and rather allow the QScrollArea to grow as much as the column allows. Once the elements are drawn, they're fixed (no resizing).
So, basically, how can I find the size of a column in QGridLayout? Once I find that, I can limit the widths accordingly.
Thanks!

If all you want is to avoid displaying horizontal scroll bars within a QScrollArea, simply call
QScrollArea::setHorizontalScrollBarPolicy()
with Qt::ScrollBarAlwaysOff. To get the width and height of a QGridLayout cell, use the following code:
QSize getLayoutCellSize(QGridLayout *layout, int row, int column)
{
QLayoutItem *item = layout->itemAtPosition(row, column);
if (item)
return (item->sizeHint());
return (QSize());
}
You may can also use QLayoutItem::geometry() instead of QLayoutItem::sizeHint().

Related

Match width of largest widget in Column in QGridLayout

I have a simple QGridLayout of different QGroupBox widgets. These QGroupBox Widgets could have variable wides (depending on how much content goes in each one.
When stacking the groupboxes into a grid, the next column starts at the right of the longest groupbox, but if there are shorter groupboxes in the same column, there will be empty space.
Is there an approach to basically force every single widget in a column to be the width of the maximum width widget?
Basically I want a column to go from this:
<===== Widget 1 Width =======>
<== Widget 2 Width ==> (empty)
<=Widget 1 Width=> (empty )
To this:
<===== Widget 1 Width =======>
<===== Widget 2 Width =======>
<===== Widget 3 Width =======>
Instead of leaving empty space before the next column, force the groupbox (or whatever widget is in the grid) to fill to the end. Right now the column just ends at whatever the largest width is, I still want that to happen, but I want the smaller widgets to fill to that size as well
Thanks!
did you try to change the size policy of widgets in the layout to expanding?
according to the document :
QSizePolicy::Expanding : The sizeHint() is a sensible size, but the widget can be shrunk and still be useful. The widget can make use of extra space, so it should get as much space as possible (e.g. the horizontal direction of a horizontal slider).

Set minimum row-height of QComboBox

I am trying to set the minimum height of the rows in the QComboBox dropdown menu without changing their width or the size of the QComboBox itself.
By default the width and height of the row items are calculated by its data. To make the dropdown list resize to the width of the data I am calling
view()->setMinimumWidth(view()->sizeHintForColumn(0);
in the combobox's ::showPopup().
However, since I have different font sizes in rows, I would like to enforce a minimum height on each row.
I tried to use setSizeHint on each QStandardItem that is added in the first column to the rows but without success. I tried:
item->setSizeHint(QSize(item->sizeHint().width(), minHeight));
(https://stackoverflow.com/a/10749345/1981832)
But this lead to the dropdown menu not resizing to the data's width.
So, I tried to leave the width "unset". However, both
item->setSizeHint(QSize(QSize().width(), minHeight));
and
item->setSizeHint(QSize(0, minHeight));
did not work. Does anyone have an idea how I can enforce a minimum height on the rows of the QComboBox without messing up the automatic width calculation?
Just use this lines:
QComboBox combo;
QListView *view = new QListView(&combo);
view->setStyleSheet("QListView::item{height: 100px}");
combo.setView(view);
Or write this code in qss file:
QListView::item {
height: 30px;
}
After that use:
QComboBox::setView(QAbstractItemView *itemView)

How can I get rid of vertical spacing of empty rows in a QGridLayout in Qt?

So I have a dialog that consists of one QGridLayout that has two columns of widgets (labels and comboboxes). Depending on the selections of the comboboxes some rows might be hidden.
I figured out that having the dialog call self.layout().setSizeConstraint(QLayout.SetFixedSize) as it shows/hides the comboboxes would make the dialog change size accordingly.
But then I realized that the layout was still showing the vertical spacing of empty rows, thus making the dialog show too much space here and there.
How can I get rid of this? Is there a way to have the layout resize to show only vertical spacing of rows that have visible widgets?
I think I found the solution. Using QVBoxLayout instead of QGridLayout somehow makes widgets and their vertical spacing go away when a widget is hidden.
You might need to use QLayout::takeAt ( int index ) to take out the item, once the visibility is set to false & use QLayout::addItem ( QLayoutItem * item ) when you need it back in your layout.
Keep in mind that if an item is removed, other items will be renumbered. So you have to plan what you do accordingly. Refer the documentation .

Make QTableWidget expand when dialog window size is changed

I have a QTableWidget, QTextEdit, and cancel & okay buttons.
I want these widgets to stay in the same position relative to each other, and the QTableWidget to expand if the dialog window is expanded or size changed...
How can I do that?
You need to look into Qt's layout system - using layouts will handle automatically resizing your objects based on the size of their parent.
A combination of using QWidget::setSizePolicy() and QBoxLayout::setStretch() (or more likely QBoxLayout::insertWidget(..., int stretch = 0, ...)) will allow you to acheive the behaviour you refer to where only certain objects expand to fill available space, while others remain a constant size.
Addressing the image you've given above as an example:
Aside from dragging and dropping objects into the form, to achieve this solution I have:
Set the vertical sizePolicy of textEdit to Fixed.
Set a height in textEdit's minimumSize for the sizePolicy to use.
Set layoutStretch in centralWidget to 1,0, i.e. assign the minimum possible space for the elements contained in horizontalLayout and give any remaining space to tableWidget.

Widgets next to each other same size in Qt

I have two widgets, one arbitrary (usually a QLineEdit), and one QLabel which displays a Pixmap. They are placed next to each other with a QHBoxLayout. The widget with this layout is in turn placed in another layout.
Now, what I want is that the label with the pixmap is automatically resized so that it is as high as the arbitrary widget next to it. However, even when I set the label's sizePolicy to Maximum, it still seems to expand to the original pixmap size, instead of resizing the pixmap and shrinking to match the other widget. Instead of having two equally large widgets I have the arbitrary one which is smaller than the pixmap next to it.
Any ideas how to get the size of the pixmap label to match the size of the widget next to it?
How about:
int height = arbitraryWidget->height(); // get desired height.
label->setSizeHint(QSize(label->width(), height); // set size hint to current width and desired height.
label->setSizePolicy(QSizePolicy::Fixed); // optional, but this ensures size is desired.

Resources