开发者

QGridLayout: Getting the list of QWidget added

Suppose I have something like:

QGridLayout layout;
layout.addWidget(new QWidget());
layout.addWidget(new QWidget());

What is the method that I can use 开发者_如何学JAVAto get the list of the added QWidgets ?

Something like the imaginary getAddedWidgets() below:

QList<QWidget*> addedWidgets = layout.getAddedWidgets(); 
Q_ASSERT( addedWidgets.size() == 2 );


The example code below shows how you could iterate over each item:

  QGridLayout layout;

  // add 3 items to layout
  layout.addItem(new QSpacerItem(2,3), 0, 0, 1, 1);
  layout.addWidget(new QWidget);
  layout.addWidget(new QWidget);

  // sanity checks
  Q_ASSERT(layout.count() == 3);
  Q_ASSERT(layout.itemAt(0));
  Q_ASSERT(layout.itemAt(1));
  Q_ASSERT(layout.itemAt(2));
  Q_ASSERT(layout.itemAt(3) == NULL);

  // iterate over each, only looking for QWidgetItem
  for(int idx = 0; idx < layout.count(); idx++)
  {
    QLayoutItem * const item = layout.itemAt(idx);
    if(dynamic_cast<QWidgetItem *>(item))       <-- Note! QWidgetItem, and not QWidget!
      item->widget()->hide();                   <-- widget() will cast you a QWidget!
  }

  // without using RTTI !
  for(int idx = 0; idx < layout.count(); idx++)
  {
    QLayoutItem * const item = layout.itemAt(idx);
    if(qobject_cast<QWidgetItem *>(item))       <-- Qt way of cast! No RTTI required!
      item->widget()->hide();
  }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜