To hide all widgets in a grid layout
-
There are several layouts in the grid layout, and there are several widgets in it, so is there a way to access the parent layout and hide all widgets in the sublayout?
void AA::LayoutVisible(QLayout* layout, bool bShow, int nIndex) { int ncount = layout->count(); /*if (layout->count() < nIndex) return;*/ /*if (layout->count() == 1) return;*/ for (int i = 0; i < layout->count(); i++) { QLayoutItem* pItem = layout->itemAt(i); if( pItem->widget() ) pItem->widget()->hide(); } //layout->itemAt(nIndex)->widget()->setVisible(bShow); }
I turned the Gridrayout pointer over to the factor in my way, and there was a null pointer error.
-
If it was me I would put the gridlayout in its own widget and setvisible(false) the top level widget instead of trying to traverse the tree and inspect all the gridlayout items. Keep in mind that hiding a widget will probably have consequences on the higher level layout during redraw so your size policies need to be correct.
-
@IknowQT
As @Kent-Dorfman says it would seem neater if you hid the whole of a widget containing the layout.But if you do want to do it your way:
I turned the Gridrayout pointer over to the factor in my way, and there was a null pointer error.
Use a debugger to see where it "crashes". I am wondering:
QLayoutItem* pItem = layout->itemAt(i); if( pItem->widget() )
I would check for
pItem != nullptr
.