4

我想知道是否可以隐藏垂直布局。我目前有一个带有两个垂直布局的水平布局。我想在单击按钮时隐藏一个垂直布局(及其所有内容)。关于我如何做到这一点的任何建议。

4

3 回答 3

5

As @jmk said, you need to use a QWidget. I'll just add that it's very easy to turn an existing horizontal or vertical layout into a widget from Qt Designer by right-clicking on it and selecting Morph Into->QWidget:

enter image description here

The layout is entirely preserved, but now you can show/hide the layout box because it's an ordinary widget with that layout.

于 2016-08-29T08:43:30.737 回答
2

无需将垂直布局直接插入顶级水平布局,而是使用容器小部件轻松控制可见性:

// Create your left and right widgets
QWidget* leftWidget = new QWidget();
QVBoxLayout* leftLayout = new QVBoxLayout(leftWidget);
QWidget* rightWidget = new QWidget();
QVBoxLayout* rightLayout = new QVBoxLayout(rightWidget);

// Populate your vertical layouts here ...

QHBoxLayout* horizontalLayout = new QHBoxLayout(parentWidget);
horizontalLayout->addWidget(leftWidget);
horizontalLayout->addWidget(rightWidget);

然后,您可以简单地隐藏或显示leftWidgetrightWidget有效控制您拥有的垂直布局中所有内容的可见性,而无需隐藏/显示每个单独的小部件。

于 2013-07-26T21:48:12.353 回答
1

我的建议:

// l is the layout pointer
for (int i = 0; i != l->count(); ++i) {
  QWidget* w = qobject_cast<QWidget*>(l->itemAt(i));
  if (w != 0) {
    w->setVisible(false); // hides the widget
  }
  else {
    // do some recursive things with the layout
  }
}

(希望它有效;))

小部件基本上是不可见的。

于 2013-07-26T20:19:12.663 回答