我有一个Gtk::TreeView
,我添加了一个模型和一个自定义列:
#ifndef GTKMM_EXAMPLEWINDOW_H
#define GTKMM_EXAMPLEWINDOW_H
#include <gtkmm.h>
class ExampleWindow
: public Gtk::Window
{
public:
ExampleWindow(void);
virtual ~ExampleWindow(void);
//My column
class foocol
: public Gtk::TreeView::Column
{
public:
Gtk::CellRendererToggle fooRenderer;
bool test;
foocol(void);
~foocol(void);
foocol(const foocol&);
foocol &operator=(const foocol&);
private:
//signal
void foocolData(Gtk::CellRenderer*,const Gtk::TreeModel::iterator&);
void foocolToggled(const Glib::ustring&);
};
//Tree model columns:
class ModelColumns : public Gtk::TreeModel::ColumnRecord
{
public:
ModelColumns(void){ add(m_col_id); add(m_col_name); }
Gtk::TreeModelColumn<unsigned int> m_col_id;
Gtk::TreeModelColumn<Glib::ustring> m_col_name;
Gtk::TreeModelColumn<ExampleWindow::foocol> m_col_foocol;
};
foocol m_foocol; // XXX
ModelColumns m_Columns;
//Child widgets:
Gtk::TreeView m_TreeView;
Glib::RefPtr<Gtk::ListStore> m_refTreeModel;
};
#endif //GTKMM_EXAMPLEWINDOW_H
#include <iostream>
#include "examplewindow.hh"
#define CB_FUNCTION(X) sigc::mem_fun( this, &ExampleWindow::foocol::X )
#define CONNECT(X) connect( sigc::mem_fun( this,\
&ExampleWindow::foocol::X ) );
ExampleWindow::ExampleWindow()
{
set_title("List of singers");
set_border_width(5);
set_default_size(400, 200);
//Add the TreeView, inside the window
add(m_TreeView);
//Create the Tree model:
m_refTreeModel = Gtk::ListStore::create(m_Columns);
m_TreeView.set_model(m_refTreeModel);
//Fill the TreeView's model
Gtk::TreeModel::Row row = *(m_refTreeModel->append());
row[m_Columns.m_col_id] = 1;
row[m_Columns.m_col_name] = "Marilyn Manson";
row = *(m_refTreeModel->append());
row[m_Columns.m_col_id] = 2;
row[m_Columns.m_col_name] = "David Vincent";
row = *(m_refTreeModel->append());
row[m_Columns.m_col_id] = 3;
row[m_Columns.m_col_name] = "Yolandi Visser";
//Add the TreeView's view columns:
//This number will be shown with the default numeric formatting.
m_TreeView.append_column( "ID", m_Columns.m_col_id );
m_TreeView.append_column( "Name", m_Columns.m_col_name );
m_TreeView.append_column( "model.m_col_foocol", m_Columns.m_col_foocol );
m_TreeView.append_column( m_foocol ); // XXX
show_all_children();
}
ExampleWindow::~ExampleWindow()
{
}
ExampleWindow::foocol::foocol( )
{
std::cout << "foocol constructor " << this << "\n";
this->set_title( "foocol" );
this->pack_start( fooRenderer );
//Tell the view column how to render the model values
this->set_cell_data_func( fooRenderer, CB_FUNCTION( foocolData ) );
fooRenderer.property_sensitive() = true; // done by default
fooRenderer.signal_toggled().CONNECT( foocolToggled );
}
ExampleWindow::foocol::~foocol( )
{
std::cout << "foocol destructor " << this << "\n";
}
ExampleWindow::foocol::foocol( const ExampleWindow::foocol& copyMe )
: test(copyMe.test)
{
std::cout << "foocol copy constructor " << this << "\n";
}
ExampleWindow::foocol&
ExampleWindow::foocol::operator=( const ExampleWindow::foocol& copyMe )
{
std::cout << "assignement " << this << " = " << ©Me << "\n";
return *this;
}
/********* SIGNALS ******/
void
ExampleWindow::foocol::foocolData( Gtk::CellRenderer* renderer
, const Gtk::TreeModel::iterator&
iter )
{
std::cout << "foocolData " << this << "\n";
}
void
ExampleWindow::foocol::foocolToggled( const Glib::ustring& path )
{
std::cout << "foocolToggled " << this << "\n";
}
#include <iostream>
#include "examplewindow.hh"
#include <gtkmm/application.h>
int main( int argc
, char *argv[] )
{
Glib::RefPtr<Gtk::Application> app
= Gtk::Application::create( argc, argv
, "org.gtkmm.example" );
ExampleWindow window;
//Shows the window and returns when it is closed.
return app->run( window );
}
g++ examplewindow.cc main.cc `pkg-config gtkmm-3.0 --cflags --libs` -std=c++11
我不是种族主义者,我只是不知道如何在Windows上编译。
foocol
的列,您可以在其中看到切换,在左侧,您可以看到名为model.mcolfoocol
的列。
第二列是我的问题所在,我根本没有看到切换。我不知道为什么,我花了几个小时来解决这个问题......
两个列都是相同的,不同的结果来自一个作为列添加的列,而第二个(没有工作的那个)是通过Gtk::TreeModel::ColumnRecord
添加的。当然我需要使用第二种方法......
最好对我添加// XXX
的行进行注释,以便控制台中的所有消息都与问题相关。
foocol constructor 0xbfda6e70
(a.out:12545): Gtk-CRITICAL **: gtk_cell_layout_add_attribute: assertion 'column >= 0' failed
foocolData 0xbfda6e70
foocolData 0xbfda6e70
foocolData 0xbfda6e70
foocolData 0xbfda6e70
foocolData 0xbfda6e70
foocolData 0xbfda6e70
foocolData 0xbfda6e70
foocolData 0xbfda6e70
foocolData 0xbfda6e70
foocol destructor 0xbfda6e70
不幸的是,我不知道如何调试使用多个库的程序。我甚至不确定我的电脑有足够的内存来做到这一点......
我真的觉得这部分gtkmm不直观......
答案 0 :(得分:2)
您可能不希望使用ExampleWindow :: foocal作为模型支持类型(在Gtk :: TreeModelColumn&lt; ExampleWindow :: foocol&gt; m_col_foocol;);您正在混合TreeModel和TreeView类型。
我不确定你的意图(为什么你不会只使用TreeModelColumn&lt; bool&gt;,它会自动呈现一个复选框)。 下面我强制您的列注册 Model&#39> ColumnRecord,并使用 View ,并显示(并生成切换事件,但不能正确切换);注意我需要改变它的类型;希望这能帮助你朝着理想的方向前进。 我已注释掉标有// XXX的行,并进行了修改:
examplewindow.h更改
//My column
class foocol
- : public Gtk::TreeView::Column
+ : public Gtk::TreeView::Column, public Gtk::TreeModelColumn<bool>
{
...
- ModelColumns(void){ add(m_col_id); add(m_col_name); }
+ ModelColumns(void){ add(m_col_id); add(m_col_name); add (m_col_foocol); }
...
- Gtk::TreeModelColumn<ExampleWindow::foocol> m_col_foocol;
+ foocol m_col_foocol;
examplewindow.cc
- m_TreeView.append_column( "model.m_col_foocol", m_Columns.m_col_foocol );
+ m_TreeView.append_column( m_Columns.m_col_foocol );