gtkmm,如何设置工具栏?

gtkmm, how to set a tool bar?

我想让我的工具栏位于 window 的顶部,而不是垂直扩展,我希望条目水平扩展。这里有一个代码:

main.cc

// g++ test2.cc `pkg-config gtkmm-3.0 --libs --cflags` -std=c++11
#include <gtkmm.h>

int main( int argc, char **argv)
{
  Glib::RefPtr< Gtk::Application >  app = Gtk::Application::create( "My.ToolBar.Drive.Me.Crazy" );

  /*window*/
  Gtk::Window * W1 = new Gtk::Window();
  W1->set_default_size(800, 600);

  /*box*/
  Gtk::Box * X1  = new Gtk::Box( Gtk::Orientation::ORIENTATION_VERTICAL );

  /*toolbar*/
  Gtk::Toolbar * T1 = new Gtk::Toolbar();

  /*button*/
  Gtk::ToolButton * B1 = new Gtk::ToolButton(Gtk::Stock::GO_BACK);

  /*button*/
  Gtk::ToolButton * B2 = new Gtk::ToolButton(Gtk::Stock::GO_FORWARD);

  /*tool item for the entry*/
  Gtk::ToolItem * I1 = new Gtk::ToolItem();

  /*entry*/
  Gtk::Entry * E1 = new Gtk::Entry();

  /*button*/
  Gtk::ToolButton * B3 = new Gtk::ToolButton(Gtk::Stock::GO_BACK);


  T1->append( *B1 );
  T1->append( *B2 );
  T1->append( *I1 );
  T1->append( *B3 );

  I1->add( *E1);

  X1->pack_start( *T1, true, true );

  W1->add( *X1 );

  W1->show_all();

  app->run( * W1 );

  delete B1;
  delete B2;
  delete B3;
  delete I1;
  delete E1;
  delete X1;
  delete W1;
}

我不明白扩展和填充之间有什么区别,有两个函数,set_hexpand()set_vexpand() 无论我尝试(最后两个小时)如何使用这些函数,我总是得到结果与上面的代码相同。

1.- 对于 X1,将展开设置为 false。
2.- 使您的工具项也可扩展。
3.- 为 X1 添加扩展器。

// c++ main.cpp -std=c++11 `pkg-config gtkmm-3.0 --libs --cflags`
#include <gtkmm.h>

int main( int argc, char **argv) {
  Glib::RefPtr< Gtk::Application >  app = Gtk::Application::create( "My.ToolBar.Drive.Me.Crazy" );
  /*window*/
  Gtk::Window * W1 = new Gtk::Window();
  W1->set_border_width (10);

  /*box*/
  Gtk::Box * X1  = new Gtk::Box( Gtk::ORIENTATION_VERTICAL );

  /*toolbar*/
  Gtk::Toolbar * T1 = new Gtk::Toolbar();

  /*button*/
  Gtk::ToolButton * B1 = new Gtk::ToolButton(Gtk::Stock::GO_BACK);

  /*button*/
  Gtk::ToolButton * B2 = new Gtk::ToolButton(Gtk::Stock::GO_FORWARD);

  /*tool item for the entry*/
  Gtk::ToolItem * I1 = new Gtk::ToolItem();
  I1->set_expand (); // true is default

  /*entry*/
  Gtk::Entry * E1 = new Gtk::Entry();

  /*button*/
  Gtk::ToolButton * B3 = new Gtk::ToolButton(Gtk::Stock::GO_BACK);

  /* vertical expander for the window */
  Gtk::Box *expander = Gtk::manage (new Gtk::Box (Gtk::Orientation::ORIENTATION_VERTICAL));

  T1->append( *B1 );
  T1->append( *B2 );
  I1->add (*E1);
  T1->append( *I1 );
  T1->append( *B3 );



  X1->pack_start( *T1, false, true );
  X1->pack_start( *expander, true, true );

  W1->add( *X1 );

  W1->show_all();

  app->run( * W1 );

  delete B1;
  delete B2;
  delete B3;
  delete I1;
  delete E1;
  delete X1;
  delete W1;
}