Glade / gtkmm TreeView [英] Glade/gtkmm TreeView

查看:194
本文介绍了Glade / gtkmm TreeView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在跟进我对此问题的上一个问题的答案后,我已经取得了一些进展,并寻找下一个指导。简而言之,我无法使用gtkmm应用程序从Glade加载Treeview。



开始,这里是源代码。类:

  typedef struct 
{
Gtk :: Window * window_info;
Gtk :: TreeView * treeview_info;
Glib :: RefPtr< Gtk :: ListStore> liststore_info;

class InfoColumns:public Gtk :: TreeModel :: ColumnRecord
{
public:
InfoColumns(){add(time); add(message); }

Gtk :: TreeModelColumn< string>时间;
Gtk :: TreeModelColumn< string>信息;
} info_columns;

} UiElements;

class GuiManager
{
Glib :: RefPtr< Gtk :: Builder>构建器
UiElements elements;

public:
GuiManager();

void info_handler(string msg);
};

定义:

  GuiManager :: GuiManager()
{
builder = Gtk :: Builder :: create();
builder-> add_from_file(GUI3.glade);

builder-> get_widget(window_info,elements.window_info);
builder-> get_widget(treeview_info,elements.treeview_info);

//这两种方法加载liststore看起来像是一样的
elements.liststore_info = Glib :: RefPtr< Gtk :: ListStore> :: cast_dynamic(builder-> get_object liststore_info));
// elements.liststore_info = Gtk :: ListStore :: create(elements.info_columns);

elements.treeview_info-> set_model(elements.liststore_info);

//如果我添加列,数据出现在列表的末尾
// elements.treeview_info-> append_column(Time,elements.info_columns.time);
// elements.treeview_info-> append_column(Message,elements.info_columns.message);
}

void GuiManager :: info_handler(string msg)
{
Gtk :: TreeModel :: Row row = *(elements.liststore_info-> append ));
row [elements.info_columns.time] =现在;
row [elements.info_columns.message] = msg;
}

而XML:

 < object class =GtkListStoreid =liststore_info> 
< columns>
<! - column-name Time - >
< column type =gchararray/>
<! - column-name Message - >
< column type =gchararray/>
< / columns>
< / object>
< object class =GtkWindowid =window_info>
< property name =can_focus> False< / property>
< property name =titletranslatable =yes>信息< / property>
< child>
< object class =GtkScrolledWindowid =scrolledwindow_info>
< property name =visible> True< / property>
< property name =can_focus> True< / property>
< property name =shadow_type> in< / property>
< property name =min_content_width> 400< / property>
< property name =min_content_height> 600< / property>
< child>
< object class =GtkTreeViewid =treeview_info>
< property name =visible> True< / property>
< property name =can_focus> True< / property>
< property name =model> liststore_info< / property>
< property name =enable_search> False< / property>
< property name =enable_grid_lines> both< / property>
< child internal-child =selection>
< object class =GtkTreeSelectionid =treeview_info_selection/>
< / child>
< child>
< object class =GtkTreeViewColumnid =treeview_info_column_time>
< property name =resizable> True< / property>
< property name =sizing> autosize< / property>
< property name =min_width> 100< / property>
< property name =titletranslatable =yes>时间< / property>
< property name =clickable> True< / property>
< child>
< object class =GtkCellRendererTextid =treeview_info_cellrenderer_time/>
< / child>
< / object>
< / child>
< child>
< object class =GtkTreeViewColumnid =treeview_info_column_message>
< property name =resizable> True< / property>
< property name =sizing> autosize< / property>
< property name =min_width> 300< / property>
< property name =titletranslatable =yes> Message< / property>
< property name =clickable> True< / property>
< child>
< object class =GtkCellRendererTextid =treeview_info_cellrenderer_message/>
< / child>
< / object>
< / child>
< / object>
< / child>
< / object>
< / child>
< / object>

当我编译和运行这个代码(加上一个调用 info_handler )我得到一个树和一行和两个空白列。当我取消注释附加列的两行时,我得到一行和四列的树视图。前两列是空白的(根据glade文件确定大小),第二列有Now msg

我从中得到的是 elements.info_columns 未与 elements.treeview_info 通过 elements.liststore_info 相关联。它看起来像我只是失去一个步骤,连接两个。 liststore_info 被列为glade文件中 treeview_info 的模型,以及

解决方案

我想您需要设置每个表列中应显示哪个列表商店字段。


  1. 右键单击树窗口小部件,然后点击编辑... (不是单独编辑

  2. 标签

  3. 点击列

  4. 属性和属性 >,并从下拉列表中选择正确的列表存储列(将相应地更新索引)。对于Pixbuf列,您需要 Pixbuf对象而不是文本

在你的情况下,我相信你最终会得到时间列的索引为0,消息列的索引为1,所有其他字段的索引为-1。



这将从选定字段填充列的文本在列表存储。我相信它是这样做的,所以你可以填充一个列的其他属性(字体,颜色等)通过列表存储,如果你想要的。



不相关这个,我也考虑将 Gtk :: TreeModelColumn< string> 更改为 Gtk :: TreeModelColumn< Glib :: ustring> 因为我相信你现在的方式可能会导致/从 Glib :: ustring 每次树视图重绘本身的转换。保持它作为 Glib :: ustring 从开始应该避免大部分的转换。


In following up with an answer to my previous question on this issue, I've made some progress and am looking for the next bit of guidance. In brief, I'm having trouble loading up a Treeview from Glade with a gtkmm application.

To start, here's the source. The class:

typedef struct
{
  Gtk::Window *window_info;
  Gtk::TreeView *treeview_info;
  Glib::RefPtr<Gtk::ListStore> liststore_info;

  class InfoColumns : public Gtk::TreeModel::ColumnRecord
  {
    public:
      InfoColumns() { add(time); add(message); }

      Gtk::TreeModelColumn<string> time;
      Gtk::TreeModelColumn<string> message;
  } info_columns;

}UiElements;

class GuiManager
{
  Glib::RefPtr<Gtk::Builder> builder;
  UiElements elements;

  public:
    GuiManager();

    void info_handler(string msg);
};

The definition:

GuiManager::GuiManager()
{
  builder = Gtk::Builder::create();
  builder->add_from_file("GUI3.glade");

  builder->get_widget("window_info", elements.window_info);
  builder->get_widget("treeview_info", elements.treeview_info);

//these two methods of loading the liststore appear to function identically
  elements.liststore_info = Glib::RefPtr<Gtk::ListStore>::cast_dynamic(builder->get_object("liststore_info"));
//  elements.liststore_info = Gtk::ListStore::create(elements.info_columns);

  elements.treeview_info->set_model(elements.liststore_info);

//if I append the columns, the data appears at the end of the list
//  elements.treeview_info->append_column("Time", elements.info_columns.time);
//  elements.treeview_info->append_column("Message", elements.info_columns.message);
}

void GuiManager::info_handler(string msg)
{
  Gtk::TreeModel::Row row = *(elements.liststore_info->append());
  row[elements.info_columns.time] = "Now";
  row[elements.info_columns.message] = msg;
}

And the XML:

<object class="GtkListStore" id="liststore_info">
  <columns>
    <!-- column-name Time -->
    <column type="gchararray"/>
    <!-- column-name Message -->
    <column type="gchararray"/>
  </columns>
</object>
<object class="GtkWindow" id="window_info">
  <property name="can_focus">False</property>
  <property name="title" translatable="yes">Info</property>
  <child>
    <object class="GtkScrolledWindow" id="scrolledwindow_info">
      <property name="visible">True</property>
      <property name="can_focus">True</property>
      <property name="shadow_type">in</property>
      <property name="min_content_width">400</property>
      <property name="min_content_height">600</property>
      <child>
        <object class="GtkTreeView" id="treeview_info">
          <property name="visible">True</property>
          <property name="can_focus">True</property>
          <property name="model">liststore_info</property>
          <property name="enable_search">False</property>
          <property name="enable_grid_lines">both</property>
          <child internal-child="selection">
            <object class="GtkTreeSelection" id="treeview_info_selection"/>
          </child>
          <child>
            <object class="GtkTreeViewColumn" id="treeview_info_column_time">
              <property name="resizable">True</property>
              <property name="sizing">autosize</property>
              <property name="min_width">100</property>
              <property name="title" translatable="yes">Time</property>
              <property name="clickable">True</property>
              <child>
                <object class="GtkCellRendererText" id="treeview_info_cellrenderer_time"/>
              </child>
            </object>
          </child>
          <child>
            <object class="GtkTreeViewColumn" id="treeview_info_column_message">
              <property name="resizable">True</property>
              <property name="sizing">autosize</property>
              <property name="min_width">300</property>
              <property name="title" translatable="yes">Message</property>
              <property name="clickable">True</property>
              <child>
                <object class="GtkCellRendererText" id="treeview_info_cellrenderer_message"/>
              </child>
            </object>
          </child>
        </object>
      </child>
    </object>
  </child>
</object>

When I compile and run this code (plus one call to info_handler()) I get a treeview with one row and two blank columns. When I uncomment the two lines appending the columns, I get a treeview with one row and four columns. The first two columns are blank (and sized according to the glade file) and the second two have the "Now" and msg strings (and are sized automatically to the contents).

What I gather from this is that elements.info_columns is not associated with elements.treeview_info via elements.liststore_info. It looks like I'm just missing a step to connect the two. liststore_info is listed as the model for treeview_info in the glade file, as well as being set in GuiManager's constructor.

Where's the disconnect?

解决方案

I think you need to set which list store field should be displayed in each table column.

  1. In Glade, right-click on the tree widget and click Edit... (not Edit separately)
  2. Click on the Hierarchy tab
  3. Click on a column
  4. Under Properties and Attributes ensure Text is ticked and the correct list store column is chosen from the drop down list (which will update the index accordingly.) For Pixbuf columns, you want Pixbuf Object instead of Text.
  5. Repeat for each column

In your case, I believe you should end up with an index of 0 for the Time column, and 1 for the Message column, and -1 in all the other fields.

This will populate the column's text from the selected field in the list store. I believe it's done this way so you can populate a column's other attributes (font, colour, etc.) through the list store as well, if you want.

Unrelated to this, I would also consider changing Gtk::TreeModelColumn<string> to Gtk::TreeModelColumn<Glib::ustring> as I believe the way you have it now may cause a conversion to/from a Glib::ustring every time the tree view redraws itself. Keeping it as a Glib::ustring from the start should avoid most of this conversion.

这篇关于Glade / gtkmm TreeView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆