mus*_*abi 1 c++ gtk treeview glade gtkmm
在跟进我之前关于这个问题的问题的答案时,我已经取得了一些进展,并且正在寻找下一点指导.简而言之,我无法使用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> time;
Gtk::TreeModelColumn<string> message;
} info_columns;
}UiElements;
class GuiManager
{
Glib::RefPtr<Gtk::Builder> builder;
UiElements elements;
public:
GuiManager();
void info_handler(string msg);
};
Run Code Online (Sandbox Code Playgroud)
定义:
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;
}
Run Code Online (Sandbox Code Playgroud)
和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>
Run Code Online (Sandbox Code Playgroud)
当我编译并运行此代码(加上一次调用info_handler())时,我得到一个包含一行和两个空白列的树视图.当我取消注释附加列的两行时,我得到一个包含一行和四列的树视图.前两列是空白的(根据glade文件调整大小),后两列具有"Now"和msg字符串(并根据内容自动调整大小).
我从中收集到的是elements.info_columns与elements.treeview_infovia 无关elements.liststore_info.看起来我只是错过了连接两者的步骤.liststore_info被列为treeview_infoglade文件中的模型,以及在GuiManager构造函数中设置.
断开的地方在哪里?
我认为您需要设置应在每个表列中显示哪个列表存储字段.
在您的情况下,我认为最终时间列的索引应为0,Message列的索引为1,所有其他字段的索引为-1.
这将从列表存储中的选定字段填充列的文本.我相信它是这样做的,所以如果你愿意的话,你也可以通过列表存储填充列的其他属性(字体,颜色等).
与此无关,我也会考虑更改Gtk::TreeModelColumn<string>为,Gtk::TreeModelColumn<Glib::ustring>因为我相信你现在拥有它的方式可能会导致Glib::ustring每次树视图重绘自己时转换.Glib::ustring从一开始就保持它应该避免大部分转换.
| 归档时间: |
|
| 查看次数: |
1179 次 |
| 最近记录: |