How to properly clear and change layouts?
-
(Qt 4.7.3)
I am using QGraphicsGridLayout to lay out several QGraphicsWidgets.
On occasion, I want to re-arrange the items, and remove or add some items to the layout. I found that the methods provided don't exactly make this a piece of cake:
-) No clear() ? Seriously?
-) removeAt, but only with an index? What is this index supposed to mean? I didn't find it anywhere else.
-) How can I reset a row's fixed height, once I have set it?
-) Will now-unused rows and columns be removed automagically? Or will they remain as artifacts, and possibly use up space?Here is what I tried so far:
I have written my own clear():
@void CCvui_WidgetManager::clearLayout(QGraphicsLayout* pLayout)
{
if ( ! pLayout)
{
return;
}for (int i = pLayout->count()-1; i >= 0; --i) { pLayout->removeAt(i); }
}@
I have pointers to all my widgets as members, so I can temporarily handle the fact they are no longer "owned" by the layout.
Then, I have two possible layouts I want to be able to switch between:
@void CCvui_WidgetManager::arrangeLayoutForAnalogAndBinary()
{
clearLayout(m_pComponentLayout);// All items are visible in this layout variant m_pComponentLayout->addItem(m_pAnalogLegend,0,0, Qt::AlignTop | Qt::AlignHCenter); m_pComponentLayout->addItem(m_pDateLabel,1,0, Qt::AlignBottom | Qt::AlignHCenter); m_pComponentLayout->addItem(m_pBinaryLegend,2,0,1,2); m_pComponentLayout->addItem(m_pValueScale,0,1); m_pComponentLayout->addItem(m_pAnalogCurveCanvas,0,2); m_pComponentLayout->addItem(m_pTimeScale,1,2,1,1); m_pComponentLayout->addItem(m_pBinaryCurveCanvas,2,2,1,1);
}
void CCvui_WidgetManager::arrangeLayoutForBinaryOnly()
{
clearLayout(m_pComponentLayout);m_pComponentLayout->addItem(m_pBinaryLegend,0,0,1,2); m_pComponentLayout->addItem(m_pBinaryCurveCanvas,0,2,1,1); m_pComponentLayout->addItem(m_pTimeScale,1,2,1,1); m_pComponentLayout->addItem(m_pDateLabel,1,0, Qt::AlignBottom | Qt::AlignHCenter); // The following items are invisible, and therefore not added // to the layout: m_pAnalogLegend->setParentItem(m_pSpareComponents); m_pAnalogCurveCanvas->setParentItem(m_pSpareComponents); m_pValueScale->setParentItem(m_pSpareComponents); // Set a parent so they are deleted correctly.
}@
As you can see, I use the same layout, the same cells, and the same items, but I arrange them differently. In addition, in layout variant "binaryOnly", I have a few invisible items, which I must "park" in an invisible item in order to keep the ownership clear.
The only solution that looks promising I have found so far is to
-) Remove all items from the layout
-) delete the layout
-) re-create the layout
-) re-add the items to the layout
-) re-add the layout to its parentIs there any simpler, reliable way to re-arrange my items within the layout?