【问题标题】:Gtk (mm) limit width of comboboxGtk (mm) 组合框的限制宽度
【发布时间】:2017-04-14 15:03:15
【问题描述】:

因为我使用的组合框可能包含非常长的文本条目, 这导致组合框的宽度远远超出合理的大小, 我正在尝试为组合框提供最大宽度。

如果我这样做:

class MyCombo : public Gtk::ComboBox {
    private:
        CellRendererText render;
    public:
        MyCombo() {
            render.property_width_chars() = 10;
            render.property_ellipsize() = Pango::ELLIPSIZE_END;
            pack_start(render, true);
        }
};

结果将是所需宽度的空单元格,这似乎合乎逻辑,因为我没有指定要显示哪一列。但是我怎么能通过这种尝试来做到这一点呢?使用pack_start 只会绕过渲染器...

另一种方法是:

class MyCombo : public Gtk::ComboBox {
    private:
        CellRendererText render;
    public:
        MyCombo() {
            pack_start(render, true);
            set_cell_data_func(render, sigc::mem_fun(*this, &MyCombo::render_iter));
        }

        void render_iter(const TreeModel::const_iterator& iter) {
            Glib::ustring data = get_string_from_iter(iter);
            int desired_width_chars = 10; //for example
            render.property_text() = ellipsize_string(data, desired_width_chars);
        }
};

使用这种方法,它可以工作,但是弹出窗口中的文本(当您单击组合框时打开的内容)也被缩短了,这不是我想要的(显然用户应该能够阅读整个字符串而我没有关心弹出窗口的宽度。)

你能帮我解决这个问题吗?我很乐意提供任何建议/替代解决方案。

问候标签

【问题讨论】:

  • 所以你想限制组合框的宽度但不限制组合框弹出的宽度?
  • 完全正确。我对此感到非常绝望,因为这似乎是常见的行为,我只是不知道如何实现它:(

标签: gtk gtkmm


【解决方案1】:

注意: set_wrap_width 是一个函数,它将组合框中的条目总数包装在指定的列数上;它没有回答问题。

使用 set_wrap_width(1) |使用 set_wrap_width(5)


按照 Noup 的回答作为指导,我设法获得了以下代码;它直接回答了问题及其要求(C++/Gtkmm)。

    // Get the first cell renderer of the ComboBox. 
    auto v_cellRenderer = (Gtk::CellRendererText*)v_comboBox.get_first_cell();

    // Probably obsolete; Sets character width to 1.
    v_cellRenderer->property_width_chars() = 1;

    // Sets the ellipses ("...") to be at the end, where text overflows.
    // See Pango::ELLIPSIZE enum for other values.
    v_cellRenderer->property_ellipsize() = Pango::ELLIPSIZE_END;

    // Sets the size of the box, change this to suit your needs. 
    // -1 sets it to automatic scaling: (width, height).
    v_cellRenderer->set_fixed_size(200, -1);

结果(图片): Result of code

注意:取决于您执行上述代码的位置;要么所有单元格的大小相同,要么只是盒子本身(预期)。 通过实验,我发现:

  • 在父对象构造函数中:所有单元格大小都相同。
  • 在单独的函数中:只有第一个单元格(框)受到影响。

我建议您将代码放在与组合框的更改信号连接的函数中,例如:

    v_comboBox.signal_changed().connect(sigc::mem_fun(*this, &YourClass::comboBox_changedFunction));

【讨论】:

    【解决方案2】:

    这可能是您正在寻找的:

    cell_renderer_text.set_wrap_width(10) 
    

    这是针对 Python 的,但你明白了 :-)

    不幸的是,文档很少。我在 Anjuta/Glade 闲逛时发现了这个。

    编辑:

    文档是here。它们并不过分有用,但它们确实存在。

    【讨论】:

    • 那太好了,谢谢:)它就像一个魅力......但你是对的,文档是可怕的......我在询问之前花了几个小时搜索这个并且所有这些属性都没有任何文档所有...
    【解决方案3】:

    作为替代方案,以下内容对我有用,而无需设置 wrap_width 或子类 ComboBox(在 Gtk# 中):

    ComboBoxText cb = new ComboBoxText();
    cb.Hexpand = true; //If there's available space, we use it
    CellRendererText renderer = (cb.Cells[0] as CellRendererText); //Get the ComboBoxText only renderer
    renderer.WidthChars = 20; //Always show at least 20 chars
    renderer.Ellipsize = Pango.EllipsizeMode.End;
    

    注意:如果空间可用,我将使用展开来使用空间。如果您只想将组合框保持在固定宽度,只需删除该位即可。

    【讨论】:

      猜你喜欢
      • 2017-12-05
      • 1970-01-01
      • 2011-09-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-30
      相关资源
      最近更新 更多