I faced this problem when I'm adding QCPLayoutGrid to plotLayout() the graph will not expand and it will have a very thin width

            QCPLayoutGrid *subLayout = new QCPLayoutGrid;
            subLayout->setMaximumSize(QWIDGETSIZE_MAX, 30);
            CustomPlot->plotLayout()->addElement(CustomPlot->plotLayout()->rowCount(), 0, subLayout); // This is where it all happens when I comment out this part it will go back to normal
            subLayout->setMargins(QMargins(5, 0, 5, 5));
            subLayout->addElement(0, 0, CustomPlot(i, j)->legend);
            CustomPlot(i, j)->legend->setFillOrder(QCPLegend::foColumnsFirst);