如何在 Gtk 中设置 HPaned 中右窗格的最小和当前大小?

发布于 2024-12-10 06:04:56 字数 424 浏览 1 评论 0原文

我正在尝试使应用程序窗口在左窗格和右窗格之间具有可移动分隔符。 在左侧窗格中,我有一个 ListView。在右侧窗格中,我有一个带有一些标签、条目等的 VBox。 我添加了 HPaned 容器,将 ListView 添加到其左窗格,将 VBox 添加到其右窗格。 很简单。

问题就来了:

  1. 当用户调整窗口大小时,我需要使右窗格保存其大小。虽然,似乎唯一受支持的行为是左窗格执行此操作时。

  2. 我需要为右窗格设置一个最小宽度,这样如果用户尝试调整它的大小,它就不会太小,或者,由于第一个问题,只需将窗口大小调整为较小的尺寸。我没有找到任何方法可以做到这一点。实际上,我可以使用 Window.SetGeometryHints() 在右窗格上为 VBox 添加最小约束,但这是唯一的方法吗?所有这些限制都会使窗口在调整大小时变得不稳定。

I am trying to make application window with movable separator between left and right pane.
On the left pane, I have a ListView. On the right pane, I have a VBox with some labels, entries, etc.
I have added HPaned container, added ListView to its left pane and VBox to its right pane.
Pretty simple.

And there goes the problem:

  1. I need to make right pane to save its size when user resizes the window. Although, it seems the only supported behavior is when left pane does so.

  2. I need to set a minimal width for right pane so it will not be too small if the user tries to resize it, or, cuz of first problem, just resizes the window to smaller size. I did not find any way to do that. Actually, I can use Window.SetGeometryHints() to add a minimal constraint for VBox on right pane, but is it the only way to do it? All these constraints make window being jerky when I am resizing it.

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

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

发布评论

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

评论(2

笑脸一如从前 2024-12-17 06:04:56

好的,经过几次实验,我找到了第一个问题的解决方案。
对于左窗格子项调用 Pack1(widget, true, true) ,对于右窗格子项调用 Pack2(widget, false, false) 就足够了。

第二个问题的解决方案是使用 Window.SetGeometryHints() 为右窗格子项添加 MinSize 约束,但用户似乎无法缩小窗口。

如果有人知道解决第二个问题的正确方法,非常感谢您的帮助。

Okay, after few experiments I found a solution for first problem.
It is enough to call Pack1(widget, true, true) for left pane child, and Pack2(widget, false, false) for right pane child.

Solution for second problem would be to add MinSize constraint for right pane child with Window.SetGeometryHints(), but it seems what the window becomes unshrinkable by user.

If someone knows correct way to solve second problem, your help is very appreciated.

感性不性感 2024-12-17 06:04:56

使用 GTK-4.0 和 GtkPaned,您可以在最后一个子级上使用 set_size_request(minimum_width,-1)set_shrink_end_size(false),因此最终子进程请求最小大小,GtkPaned 会尊重它。

要将初始大小设置为最小值,可以使用set_position(INT_MAX)Paned 不会使最终子级小于其请求的大小,因此它会选择最大的可能位置。

使用 gtkmm-4.0 的 C++ 示例:

#include <gtkmm/application.h>
#include <gtkmm/label.h>
#include <gtkmm/paned.h>
#include <gtkmm/window.h>
#include <iostream>

class MainWindow : public Gtk::Window {
  public:
    MainWindow() {
        // init main window
        set_child(mypaned);
        // init paned
        mypaned.set_orientation(Gtk::Orientation::HORIZONTAL);
        mypaned.set_start_child(left);
        mypaned.set_end_child(right);
        mypaned.set_shrink_end_child(false);
        mypaned.set_resize_end_child(false);
        mypaned.set_wide_handle(true);
        mypaned.set_position(INT_MAX);
        // init children of paned
        right.set_size_request(200, -1);
        right.set_text("right side");
        left.set_text("left side of paned");
    };
    virtual ~MainWindow(){};

  protected:
    // Member Widgets
    Gtk::Paned mypaned;
    Gtk::Label left;
    Gtk::Label right;
};

int main(int argc, char **argv) {
    auto app = Gtk::Application::create("com.example.paned");
    return app->make_window_and_run<MainWindow>(argc, argv);
}

With GTK-4.0 and GtkPaned, you can use set_size_request(minimum_width,-1) on the end child and set_shrink_end_size(false), so the end child requests a minimum size and the GtkPaned respects it.

To set the initial size to the minimum, you can use set_position(INT_MAX). The Paned will not make the end child smaller than its requested size, so it selects the largest possible position.

Example in c++ with gtkmm-4.0:

#include <gtkmm/application.h>
#include <gtkmm/label.h>
#include <gtkmm/paned.h>
#include <gtkmm/window.h>
#include <iostream>

class MainWindow : public Gtk::Window {
  public:
    MainWindow() {
        // init main window
        set_child(mypaned);
        // init paned
        mypaned.set_orientation(Gtk::Orientation::HORIZONTAL);
        mypaned.set_start_child(left);
        mypaned.set_end_child(right);
        mypaned.set_shrink_end_child(false);
        mypaned.set_resize_end_child(false);
        mypaned.set_wide_handle(true);
        mypaned.set_position(INT_MAX);
        // init children of paned
        right.set_size_request(200, -1);
        right.set_text("right side");
        left.set_text("left side of paned");
    };
    virtual ~MainWindow(){};

  protected:
    // Member Widgets
    Gtk::Paned mypaned;
    Gtk::Label left;
    Gtk::Label right;
};

int main(int argc, char **argv) {
    auto app = Gtk::Application::create("com.example.paned");
    return app->make_window_and_run<MainWindow>(argc, argv);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文