是否可以让QSplitter
具有一个固定大小的小部件而另一个具有可变大小的小部件?
此代码设置正确,但在调整窗口大小时,拆分器会与初始窗口小部件大小成比例地更改:
vSplitter = new QSplitter(Qt::Vertical, this);
vSplitter->addWidget(widget1);
vSplitter->addWidget(widget2);
QList<int> heights;
heights.push_back(550);
heights.push_back(1000);
vSplitter->setSizes(heights);
setCentralWidget(vSplitter);
谢谢。
答案 0 :(得分:0)
我正在尝试做类似的事情。我有一个固定小部件(顶部)和一个非固定小部件(底部)。我希望用户能够正常使用拆分器句柄,但我不希望窗口大小调整为固定小部件添加空间。在两个小部件上使用setSizePolicy对我来说都不起作用,也没有在一个或两个小部件上使用setStretchFactor。
我最终继承了QSplitter,因此我可以实现一个resizeEvent,它会在拆分器调整大小后移动拆分器。此代码假定: 只有两个小部件。 top [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);
}