在回答有关此问题的previous question问题时,我已经取得了一些进展,并且正在寻找下一个指导。简而言之,我在使用gtkmm应用程序从Glade加载Treeview时遇到了麻烦。
首先,这是源。类(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);
};
定义:
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()
的一个调用)时,我得到了一个具有一行和两行空白列的树状 View 。当取消注释添加列的两行时,我将得到一个包含一行和四列的树状 View 。前两列为空白(并根据glade文件调整大小),后两列包含"Now"
和msg
字符串(并自动调整大小以适应内容)。我从中收集到的是
elements.info_columns
没有通过elements.treeview_info
与elements.liststore_info
关联。看来我只是缺少将两者连接的步骤。 liststore_info
在glade文件中被列为treeview_info
的模型,并且在GuiManager
的构造函数中进行了设置。断开连接在哪里?
最佳答案
我认为您需要设置应在每个表格列中显示哪个列表存储字段。
就您而言,我相信您应该以“时间”列的索引为0,为“消息”列的索引为1,在所有其他字段中的索引为-1。
这将从列表存储中的选定字段填充列的文本。我相信这样做是为了让您也可以通过列表存储来填充列的其他属性(字体,颜色等)。
与此无关,我还考虑将
Gtk::TreeModelColumn<string>
更改为Gtk::TreeModelColumn<Glib::ustring>
,因为我认为您现在拥有的方式可能会在每次树 View 重新绘制自身时引起Glib::ustring
的转换。从一开始就将其保留为Glib::ustring
应该可以避免大多数此类转换。关于c++ - Glade/gtkmm TreeView,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/32530367/