gtkmm,如何设置工具栏?

时间:2015-01-31 17:06:34

标签: c++ linux gtkmm

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

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 个答案:

答案 0 :(得分:0)

1.-对于X1,将expand设置为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;
}