March 22, 2012

Asperamanca Asperamanca
Hobby Entomologist
373 posts

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():

  1. void CCvui_WidgetManager::clearLayout(QGraphicsLayout* pLayout)
  2. {
  3.     if ( ! pLayout)
  4.     {
  5.         return;
  6.     }
  7.  
  8.     for (int i = pLayout->count()-1; i >= 0; --i)
  9.     {
  10.         pLayout->removeAt(i);
  11.     }
  12. }

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:

  1. void CCvui_WidgetManager::arrangeLayoutForAnalogAndBinary()
  2. {
  3.     clearLayout(m_pComponentLayout);
  4.  
  5.     // All items are visible in this layout variant
  6.     m_pComponentLayout->addItem(m_pAnalogLegend,0,0,
  7.                                 Qt::AlignTop | Qt::AlignHCenter);
  8.     m_pComponentLayout->addItem(m_pDateLabel,1,0,
  9.                                 Qt::AlignBottom | Qt::AlignHCenter);
  10.     m_pComponentLayout->addItem(m_pBinaryLegend,2,0,1,2);
  11.     m_pComponentLayout->addItem(m_pValueScale,0,1);
  12.     m_pComponentLayout->addItem(m_pAnalogCurveCanvas,0,2);
  13.     m_pComponentLayout->addItem(m_pTimeScale,1,2,1,1);
  14.     m_pComponentLayout->addItem(m_pBinaryCurveCanvas,2,2,1,1);
  15. }
  16.  
  17. void CCvui_WidgetManager::arrangeLayoutForBinaryOnly()
  18. {
  19.     clearLayout(m_pComponentLayout);
  20.  
  21.     m_pComponentLayout->addItem(m_pBinaryLegend,0,0,1,2);
  22.     m_pComponentLayout->addItem(m_pBinaryCurveCanvas,0,2,1,1);
  23.     m_pComponentLayout->addItem(m_pTimeScale,1,2,1,1);
  24.     m_pComponentLayout->addItem(m_pDateLabel,1,0,
  25.                                 Qt::AlignBottom | Qt::AlignHCenter);
  26.     // The following items are invisible, and therefore not added
  27.     // to the layout:
  28.     m_pAnalogLegend->setParentItem(m_pSpareComponents);
  29.     m_pAnalogCurveCanvas->setParentItem(m_pSpareComponents);
  30.     m_pValueScale->setParentItem(m_pSpareComponents);
  31.     // Set a parent so they are deleted correctly.
  32. }

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 parent

Is there any simpler, reliable way to re-arrange my items within the layout?

0 replies

 
  ‹‹ Regarding mouse move events      [SOLVED] Conversion from ’QByteArray’ to ’const uchar*’ is ambiguous? ››

You must log in to post a reply. Not a member yet? Register here!