View Full Version : Make column widths equal when concatenating 2 QGridLayouts

18th January 2016, 17:55
I have an application which uses QGridLayout, and often can require 200 to 300 rows. We found layout issues (i.e. the widget would not render) when attempting to use that many rows; QGridLayout appears to have a limit of around 100 rows. We never found a way around that issue, so we append a new QGridLayout when the prior one reaches 100 rows. The problem is that the first gridlayout has a header row which is driving the column width. The appended gridlayouts don't have that header row, therefore their column widths do not match those of the first gridlayout.

I tried the following approach to synchronize the column widths:

auto oldGridLayout = qobject_cast<QGridLayout *>(m_gridLayouts.at(static_cast<unsigned>(m_gridLayouts.size() - 2)));
auto newGridLayout = qobject_cast<QGridLayout *>(m_gridLayouts.at(static_cast<unsigned>(m_gridLayouts.size() - 1)));
for (int i = 0; i < oldGridLayout->columnCount(); i++) {
auto oldLayoutItem = oldGridLayout->itemAtPosition(0, i);
auto newLayoutItem = newGridLayout->itemAtPosition(0, i);

The above approach did not work. Investigating a bit more, geometry().width() was not giving an accurate value. Perhaps I was queering prior to the widget had enough knowledge about itself (i.e. not fully rendered), but I don't see a different approach since both widgets are rendered in the same event.

The app using these appended gridlayouts is stable and not much will be spent on it for future improvements. I'm hoping to find a solution which won't involve major surgery. Any suggestions appreciated.

18th January 2016, 20:13
If you are doing this in the constructor, then the sizes will not be correct yet. It needs to be done usually in the resize event (when the widget is visible - isVisible() returns true or after a showEvent() has occurred). Multiple resize events can occur before a widget is first shown, as the layout manager goes through the initial window layout calculations. Your algorithm might also be getting messed up if there are "stretch" elements that automatically resize grid cells based on content.

Don't know your application, but it would seem that a table view / widget with appropriate delegates might be a better solution than massive grid layouts. But this is likely a "major surgery" scenario.

19th January 2016, 05:47
This might help: