我有一个QWidget
窗口,其中包含QSplitter
。我在分割器的左侧和右侧都有QStackedLayout
(在QWidget
容器内)。主应用程序位于左侧区域,可以触发QWebViews
出现在右侧。当我删除右QWebView
时,窗口会尝试调整大小,但保持在QWebView
的宽度。当我尝试拖动窗口时,它会捕捉到分割器左侧的位置。
我希望窗口快速恢复,只显示分隔符的左侧,从右侧QStackedWidget
删除最后一个小部件。我尝试了各种各样的咒语,但我不太了解Qt,并且正在尝试学习。
下面的代码是Java,使用Jambi包装器。 C ++中的概念应该是相同的,如果有必要,我可以将任何其他语言绑定转换为Jambi。
我尝试在添加和删除小部件时将窗口最小宽度设置为RightStackedLayout
,但在某些情况下会破坏拆分器句柄。我认为这是一个黑客的解决方法。如果有人能指出我正确的方向更改分割器的右侧更新顶级窗口,我将非常感激。
// The main Window class
public Window() { // #QWidget
super();
this.setContentsMargins(0, 0, 0, 0);
QHBoxLayout mainLayout = new QHBoxLayout(this);
mainLayout.setContentsMargins(0, 0, 0, 0);
splitter = new QSplitter();
QWidget leftContainer = new QWidget();
QWidget rightContainer = new QWidget();
leftLayout = new QStackedLayout();
rightLayout = new RightStackedLayout();
leftContainer.setLayout(leftLayout);
rightContainer.setSizePolicy(new QSizePolicy(QSizePolicy.Policy.Expanding,QSizePolicy.Policy.Expanding));
rightContainer.setLayout(rightLayout);
splitter.addWidget(leftContainer);
splitter.addWidget(rightContainer);
splitter.setStretchFactor(0, 0); // do not resize left
splitter.setStretchFactor(1, 1); // fully resize right
splitter.setWindowState(WindowState.WindowMaximized);
mainLayout.addWidget(splitter);
}
public void denyWidthChange() { // when right side is closed, prevent user from resizing horizontally
this.setSizePolicy(new QSizePolicy(QSizePolicy.Policy.Fixed,QSizePolicy.Policy.Preferred));
this.splitter.setSizePolicy(new QSizePolicy(QSizePolicy.Policy.Fixed,QSizePolicy.Policy.Preferred));
this.splitter.updateGeometry();
this.updateGeometry();
}
public void allowWidthChange() { // when right side open, allow user to expand horizontally
this.setSizePolicy(new QSizePolicy(QSizePolicy.Policy.Expanding,QSizePolicy.Policy.Expanding));
this.splitter.setSizePolicy(new QSizePolicy(QSizePolicy.Policy.Expanding,QSizePolicy.Policy.Expanding));
this.splitter.updateGeometry();
this.updateGeometry();
}
public void adjustSizes(int w, int h) {
this.resize(w, h);
this.splitter.resize(w, h);
}
// The RightStackedLayout
public void add(QWidget widget) {
Application.window.allowWidthChange();
Application.window.setMinimumWidth(((WebPane)widget).minWidth()+this.rememberWidth); // left side + right side
QWidget current = this.currentWidget();
if (current != null) {
this.rememberWidth = current.size().width(); // remember the user resize preferences
}
int idx = this.indexOf(widget);
if (idx == -1) {
widget.setMinimumWidth(this.rememberWidth);
this.addWidget(widget);
}
this.setCurrentWidget(widget);
}
public void remove(QWidget widget) {
Application.window.allowWidthChange();
this.removeWidget(widget);
this.update();
if (this.count() == 0) {
Log.debug("Last RightWebPane Closing: Shrinking the window");
this.rememberWidth = widget.size().width();
this.activate();
//((QWidget)this.parent()).resize(0, 0);
Application.window.setMinimumWidth(((WebPane)widget).minWidth());
Application.window.adjustSizes(0,Application.window.height());
Application.window.denyWidthChange();
}
}
答案 0 :(得分:0)
虽然我还没有完成一个功能齐全的调整大小政策,但我一直都是这样,并且弄清楚我在尝试计算时没有考虑splitter.handleWidth()
时遇到的问题总宽度。