Reputation: 33
I want to be able to add a Gtk::ListStore to a Gtk::TreeView, the treeview was implemented through glade but I have created the ListStore in C++ and added columns and rows to it.
When I run the code it shows the window and the tree_view that has been loaded from the glade file but it is empty.
mainWindow.h:
#pragma once
#include <gtkmm.h>
class MainWindow {
protected:
Gtk::Window *main_window;
Gtk::TreeView *tree_view;
Glib::RefPtr<Gtk::ListStore> list_store;
public:
void init();
class ModelColumns : public Gtk::TreeModel::ColumnRecord
{
public:
ModelColumns() { add(name); add(age); }
Gtk::TreeModelColumn<Glib::ustring> name;
Gtk::TreeModelColumn<Glib::ustring> age;
};
};
mainWindow.cpp:
#include "mainWindow.h"
void MainWindow::init() {
auto app = Gtk::Application::create("org.gtkmm.example");
auto builder = Gtk::Builder::create_from_file("test.glade");
builder->get_widget("main_window", main_window);
builder->get_widget("tree_view", tree_view);
ModelColumns columns;
list_store = Gtk::ListStore::create(columns);
tree_view->set_model(list_store);
Gtk::TreeModel::Row row = *(list_store->append());
row[columns.name] = "John";
row[columns.age] = "30";
row = *(list_store->append());
row[columns.name] = "Lisa";
row[columns.age] = "27";
app->run(*main_window);
}
main.cpp:
#include "mainWindow.h"
int main() {
MainWindow m;
m.init();
}
Upvotes: 1
Views: 1268
Reputation: 1258
tree_view->set_model(list_store);
tells tree_view
to use list_store
as its TreeModel, but it does not say what column from the list_store
to render. In fact, the default behavior is that no column would be rendered.
To render both columns, you need to ask tree_view
to append them.
// "Name" is column title, columns.name is data in the list_store
tree_view->append_column("Name", columns.name);
tree_view->append_column("Age", columns.age);
Gtkmm has an official tutorial book. This is the section on treeview. https://developer.gnome.org/gtkmm-tutorial/stable/sec-treeview.html.en
Upvotes: 0