删除子元素后调整 Qt 窗口的大小。

发布于 2024-10-18 03:06:39 字数 3488 浏览 8 评论 0原文

我有一个 QWidget 窗口,其中包含一个 QSplitter。我在分割器的左侧和右侧都有一个 QStackedLayout (在 QWidget 容器内)。主应用程序位于左侧区域,可以触发 QWebViews 出现在右侧。当我删除右侧的 QWebView 时,窗口会尝试调整大小,但仍保持 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();
    }   
}

I have a QWidget window, which contains a QSplitter. I have a QStackedLayout (within a QWidget container) on both the left and right of the splitter. The main application resides in the left region, which can trigger QWebViews to appear on the right. When I remove the right QWebView, the Window attempts to resize, but stays at the width of the QWebView. When I try to drag the window, it snaps to position around the left side of the splitter.

I want the window to snap back to only show the left side of the splitter on removing the last widget from the right QStackedWidget. I've tried various incantations, but I don't know Qt very well, and am attempting to learn as I go.

The below code is in Java, using the Jambi wrappers. Concepts should be the same in C++, and I'm able to translate any other language binding to Jambi if necessary.

I attempt to set the window minimum width on add and remove of widgets to the RightStackedLayout, but this breaks the splitter handle in some cases. I consider this a hack workaround. If anyone can point me in the right direction of having changes to the right side of the splitter update the top level window, I'll be very grateful.

// 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();
    }   
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

刘备忘录 2024-10-25 03:06:39

虽然我还没有实现功能齐全的大小调整策略,但我已经完成了大部分工作,并找出了因不考虑 splitter.handleWidth() 而遇到的问题当尝试计算总宽度时。

While I still haven't got a fully functional resizing policy happening, I'm most of the way and figured out the problem I was having had to do with not taking the splitter.handleWidth() into account when attempting to calculate the total width.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文