RE: TreeView question



You create "myLabel" locally in the constructor. So when the constructor ends the variable goes out of scope and gets destroyed.


Date: Tue, 7 Sep 2010 12:33:56 -0600
Subject: TreeView question
From: mwhite gwmail gwu edu
To: gtkmm-list gnome org

Hi All,

I am trying to set my own widget in a treeview's column, but am running into difficulties (code below). for this contrived example, i would like to replace the column header with a Gtk::Label object. however, when i do so, my label does not appear in the header and i get an runtime error on the command line: Gtk-CRITICAL **: gtk_container_remove: assertion `GTK_IS_WIDGET (widget)' failed

i'm not understanding the error. if someone can recommend modification i can make to the code, i would really appreciate that! see comments and code in file inventory_window.cpp

Thank you!
Mike


---inventory_window.h---
#ifndef INVENTORY_WINDOW_H_
#define INVENTORY_WINDOW_H_

#include <gtkmm.h>

class InventoryWindow : public Gtk::Window
{
 public:
  InventoryWindow ();
  virtual ~InventoryWindow ();

 protected:
  // tree model columns
  class TreeModelColumns : public Gtk::TreeModel::ColumnRecord
    {
    public:
      TreeModelColumns ()
        {
          add (col_item); add (col_location); add (col_amount);
        }

      Gtk::TreeModelColumn <Glib::ustring> col_item;
      Gtk::TreeModelColumn <Glib::ustring> col_location;
      Gtk::TreeModelColumn <Glib::ustring> col_amount;
    };

  TreeModelColumns columns_;

  Gtk::ScrolledWindow scrolledWindow_;
  Gtk::TreeView treeView_;
  Glib::RefPtr<Gtk::ListStore> refTreeModel_;
  Gtk::Table table_;
  Glib::RefPtr<Gtk::ActionGroup> refActionGroup_;
  Glib::RefPtr<Gtk::UIManager> refUIManager_;
  Gtk::VBox vBox_;
};

#endif /* INVENTORY_WINDOW_H_ */
---END inventory_window.h---



---inventory_window.cpp---
#include <iostream>
#include "inventory_window.h"

InventoryWindow::InventoryWindow ()
  : table_(2, 2, false)
{
  set_title ("My test program");
  set_default_size (380, 250);
  add (vBox_);

  scrolledWindow_.add (treeView_);
  scrolledWindow_.set_policy (Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC);
  table_.attach (scrolledWindow_, 0, 1, 0, 1);
  vBox_.add (table_);

  refTreeModel_ = Gtk::ListStore::create (columns_);
  treeView_.set_model (refTreeModel_);
  treeView_.set_hover_selection (true);

  Gtk::TreeModel::Row row;

  row = *(refTreeModel_->append ());
  row[columns_.col_item] = "First Item";
  row[columns_.col_location] = "5110";
  row[columns_.col_amount] = "10";

  row = *(refTreeModel_->append ());
  row[columns_.col_item] = "Second Item";
  row[columns_.col_location] = "1234";
  row[columns_.col_amount] = "2";

  row = *(refTreeModel_->append ());
  row[columns_.col_item] = "Third Item";
  row[columns_.col_location] = "9876";
  row[columns_.col_amount] = "53";

  treeView_.append_column ("Item", columns_.col_item);
  treeView_.append_column ("Location", columns_.col_location);
  treeView_.append_column ("Amount", columns_.col_amount);
 
  // replace the "default" widget with a Gtk::Label for column 0
  // This seems to be my problem. after compiling this, when i run it,
  // i get: Gtk-CRITICAL **: gtk_container_remove: assertion `GTK_IS_WIDGET (widget)' failed
  // displayed on the command line. and the first column header is blank. i was expecting
  // it to say "hello world"
  // any ideas?
  Gtk::Label myLabel ("hello world");
  treeView_.get_column (0)->set_widget (myLabel);

  show_all_children ();
}

InventoryWindow::~InventoryWindow ()
{

}
---END inventory_window.cpp---



---main.cpp---
#include <gtkmm/main.h>
#include "inventory_window.h"

int main (int argc, char *argv[])
{
  Gtk::Main kit (argc, argv);

  InventoryWindow window;
  Gtk::Main::run (window);

  return 0;
}
---END main.cpp---


_______________________________________________ gtkmm-list mailing list gtkmm-list gnome org http://mail.gnome.org/mailman/listinfo/gtkmm-list


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]