我正在嘗試做類似的事情。我有一個固定小部件(頂部)和一個非固定小部件(底部)。我希望用戶能夠正常使用分離器句柄,但我不希望窗口大小調整爲固定小部件添加空間。在兩個小部件上使用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);
}