在回答關於此問題的我的previous question的回答後,我已經取得了一些進展,正在尋找下一個指導。簡而言之,我無法通過gtkmm應用程序從Glade加載Treeview。Glade/gtkmm 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> time;
Gtk::TreeModelColumn<string> message;
} info_columns;
}UiElements;
class GuiManager
{
Glib::RefPtr<Gtk::Builder> 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);
//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;
}
和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>
當我編譯並運行該代碼(加上一個調用info_handler()
)我得到一個TreeView一行兩空白欄。當我取消註釋添加列的兩行時,我得到一行和四列的樹視圖。前兩列爲空白(根據glade文件大小設置),後兩列的字符串爲"Now"
和msg
(並根據內容自動調整大小)。
我從這個收集是elements.info_columns
不通過elements.liststore_info
與elements.treeview_info
有關。看起來我只是錯過了連接兩者的步驟。 liststore_info
在林肯檔案中被列爲treeview_info
的模型,以及在GuiManager
的構造函數中設置。
斷開連接在哪裏?
就是這樣!最後。非常感謝。 =) – musasabi
請注意,gtkmm維護者(例如我)並不希望您能夠在glade中定義TreeModel以與gtkmm一起使用。如果它有效,那麼你就是幸運。 – murrayc