我有一个QWidget,它可以在创建和显示子窗口小部件后获得它们。它最终变得拥挤,像这样:
我尝试了QWidget::updateGeometry
,QLayout::invalidate
和QLayout::activate
,但没有效果。
这是代码的大纲。
void MainWindow :: show_dialog ()
{
QDialog d;
auto l = new QHBoxLayout (& d);
auto tabs = new QTabWidget ();
l -> addWidget (tabs);
auto w = new MyWidget ();
tabs -> addTab (w, tr ("..."));
d .exec ();
}
MyWidget :: MyWidget ()
{
auto l = new QGridLayout (this);
m_scroll_area = new QScrollArea ();
m_scroll_contents = new QWidget ();
m_layout = new QGridLayout (m_scroll_contents);
l -> addWidget (m_scroll_area, 0, 0, 1, 2);
for (auto i : things ())
add (i);
m_scroll_area -> setWidget (m_scroll_contents);
}
void MyWidget :: add (foo)
{
m_layout -> addWidget (x(foo), m_row);
m_layout -> addWidget (y(foo), m_row);
++m_row;
// This bit was added in an attempt to fix the problem,
// it does nothing.
for (QWidget * w = m_scroll_contents; w; w = w -> parentWidget ())
{
qWarning ("%p : %p", (void *)w, (void *)w->layout ());
w -> updateGeometry ();
if (w -> layout ())
{
w -> layout () -> invalidate ();
w -> layout () -> activate ();
}
w -> updateGeometry ();
}
}
我的想法是稍后可以调用MyWidget::add
来添加另一行,几何体会重新计算,但我似乎无法解决这个问题。
有些令人惊讶的是,qWarning
行表示并非根据根目录的每个小部件都有布局。我不知道这是否相关。输出是这样的:
0xa60a40 : 0xa607c0 // m_scroll_contents which has QGridLayout
0x82bb70 : 0x0 // m_scroll_area I assume
0x9aec80 : 0x0 // MyWidget, I assume, but this should have a QGridLayout (!)
0xa5e9c0 : 0xa33370 // In which case this is QTabWidget
0x9b42d0 : 0x8b7800 // QDialog ?
0x9aca30 : 0x0 // ?
0x7fffd45d0950 : 0xa262a0 // ?
答案 0 :(得分:1)
By default,QScrollArea
不会调整其包含的小部件(在您的情况下为m_scroll_contents
)。您应该致电m_scroll_area->setWidgetResizable(true);
以启用此行为。