我正在尝试做类似的事情。我有一个固定小部件(顶部)和一个非固定小部件(底部)。我希望用户能够正常使用分离器句柄,但我不希望窗口大小调整为固定小部件添加空间。在两个小部件上使用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);
}