我有一个QWidget
窗口,其中包含一个QSplitter
. 我在分离器的左侧和右侧都有一个QStackedLayout
(在容器内)。QWidget
主应用程序位于左侧区域,可以触发QWebViews
出现在右侧。当我删除 rightQWebView
时,Window 尝试调整大小,但保持在QWebView
. 当我尝试拖动窗口时,它会捕捉到拆分器左侧的位置。
我希望窗口在从右侧删除最后一个小部件时仅显示拆分器的左侧QStackedWidget
。我尝试了各种咒语,但我不太了解 Qt,并且正在尝试边走边学。
下面的代码是在 Java 中,使用 Jambi 包装器。C++ 中的概念应该是相同的,如有必要,我可以将任何其他语言绑定翻译到占碑。
我尝试在添加和删除小部件时设置窗口最小宽度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();
}
}