我正在尝试做类似的事情。我有一个固定小部件(顶部)和一个非固定小部件(底部)。我希望用户能够正常使用拆分器句柄,但我不希望调整窗口大小来为固定小部件添加空间。在两个小部件上使用 setSizePolicy 对我不起作用,在一个或两个小部件上使用 setStretchFactor 也没有。
我最终将 QSplitter 子类化,以便我可以实现一个 resizeEvent ,它会在拆分器调整大小后移动拆分器。此代码假定: 只有两个小部件。顶部 [0] 是可调整大小的,底部 [1] 不应该是。
这是我写的 resizeEvent:
MySplitter::resizeEvent(QResizeEvent *event) {
/* The first resizeEvent is -1 for height and width, because it was
invisible before. */
if (event->oldSize().height() != -1) {
int diff;
QList<int> previousSizes = sizes();
QSplitter::resizeEvent(event);
QList<int> newSizes = sizes();
/* The bottom widget is the fixed one, so find out if that is to
grow or shrink. */
diff = newSizes[1] - previousSizes[1];
if (diff > 0) {
/* Keep the bottom from growing by giving the size to the top. */
newSizes[0] += diff;
newSizes[1] -= diff;
}
else {
/* Steal size from the top to keep it from shrinking. */
newSizes[0] -= diff;
newSizes[1] += diff;
}
setSizes(newSizes);
}
else
QSplitter::resizeEvent(event);
}