gtk menuitem树结构

时间:2017-05-18 10:05:16

标签: menuitem menubar gtkmm

您好我在使用GTKmm编写应用程序,但是我无法从get_children访问某些menuitem。

您可以在下面的图片中看到幻灯片设计界面中的屏幕截图和布局

enter image description here

主GUI GTKApplicationWindow由gtkbuilder加载,然后它的指针转到递归函数以打印出来。

void loopchildren(Gtk::Widget* parent, int level){
    level++;
    if (GTK_IS_CONTAINER(parent->gobj())){
        std::cout << std::string(level, '.') << "is container level " << level << " name: " <<parent->get_name() << std::endl;
        Gtk::Container* contain = dynamic_cast<Gtk::Container*>(parent);
        std::vector<Gtk::Widget*> childrens = contain->get_children();
        std::cout << std::string(level, '.') << "starts loop level " << level << std::endl;
        for(auto const& value: childrens){
            loopchildren(value, level);
        }
    }else{
        std::cout << std::string(level, '.') << "is not container level " << level << " name: "<< parent->get_name() << std::endl;
    }
}
MyApplicationWindow::MyApplicationWindow(Glib::RefPtr<Gtk::Builder> builder, Glib::ustring baseWidgetName){
    thisBaseWidgetPointer = this;
    builder->get_widget(baseWidgetName, thisBaseWidgetPointer);
    loopchildren(thisBaseWidgetPointer, 0);
}

它产生了

以下的输出
  

.is容器级别1名称:gtkmm__GtkApplicationWindow

     

.starts循环级别1

     

..是容器级别2名称:gtkmm__GtkBox

     

..启动循环级别2

     

...是容器级别3名称:gtkmm__GtkMenuBar

     

...开始循环级别3

     

....是容器级别4名称:gtkmm__GtkMenuItem

     

....开始循环级别4

     

.....不是容器级别5名称:GtkAccelLabel

     

....是容器级别4名称:gtkmm__GtkMenuItem

     

....开始循环级别4

     

.....不是容器级别5名称:GtkAccelLabel

     

....是容器级别4名称:gtkmm__GtkMenuItem

     

....开始循环级别4

     

.....不是容器级别5名称:GtkAccelLabel

     

...是容器级别3名称:gtkmm__GtkToolbar

     

...开始循环级别3

     

...是容器级别3名称:gtkmm__GtkNotebook

     

...开始循环级别3

     

........

所以我可以看到get_children进入MenuBar下面的1级,并找到了第一级menuItems,但是在menuitem下面他们只看到了AccelLabel,但没有看到子菜单

因此,为了进一步研究这个问题,我通过信号

将其中一个子菜单项连接到一个函数
Gtk::MenuItem* menuItem;
Gtk::MenuItem* menuItem2;
Gtk::MenuBar* mmbar;
void add2(){
    std::cout << (menuItem->is_ancestor(*mmbar) ? "is ancestor" : "not ancestor ") <<std::endl;
    std::cout << menuItem->get_parent()->get_parent()->get_name() <<std::endl;
    loopchildren(menuItem->get_parent()->get_parent(), 0);
}
MyApplicationWindow::MyApplicationWindow(){
  builder->get_widget("main_menu_bar", mmbar);
  builder->get_widget("menu_basic_company", menuItem);
  menuItem->signal_activate().connect(sigc::ptr_fun(&add2));
}

问题浮出水面,主菜单栏是子菜单的“不是祖先”。相反,子菜单项的父级的父级被证明是一个Gtk :: Window,因为我认为没有明显的原因,我甚至没有在glade ui中的任何地方指定一个Gtk :: window。那个Gtk ::窗口似乎是最顶层的祖先,因为->get_parent()产生了更多错误。

  

.is容器级别1名称:GtkWindow

     

.starts循环级别1

     

..是容器级别2名称:gtkmm__GtkMenu

     

..启动循环级别2

     

...是容器级别3名称:gtkmm__GtkMenuItem

     

...开始循环级别3

     

....不是容器级别4名称:GtkAccelLabel

他们是否改变了导致此问题的Gtk中的某些内容?或者我没有正确阅读一些文件?这对我来说似乎完全违反直觉。问题可能是由于缺少小部件ID吗?

原始.ui文件的摘录

<object class="GtkApplicationWindow" id="window1">
    <property name="can_focus">False</property>
    <child>
      <object class="GtkBox">
        <property name="visible">True</property>
        <property name="can_focus">False</property>
        <property name="orientation">vertical</property>
        <child>
          <object class="GtkMenuBar" id="main_menu_bar">
            <property name="visible">True</property>
            <property name="can_focus">False</property>
            <child>
              <object class="GtkMenuItem" id="menu_sys">
                <property name="visible">True</property>
                <property name="can_focus">False</property>
                <property name="label" translatable="yes">系統</property>
                <property name="use_underline">True</property>
                <child type="submenu">
                  <object class="GtkMenu">
                    <property name="visible">True</property>
                    <property name="can_focus">False</property>
                    <child>
                      <object class="GtkMenuItem">
                        <property name="visible">True</property>
                        <property name="can_focus">False</property>
                        <property name="label" translatable="yes">系統設定</property>
                        <property name="use_underline">True</property>
                      </object>
                    </child>
                    <child>
                      <object class="GtkSeparatorMenuItem">
                        <property name="visible">True</property>
                        <property name="can_focus">False</property>
                      </object>
                    </child>
                    <child>
                      <object class="GtkMenuItem">
                        <property name="visible">True</property>
                        <property name="can_focus">False</property>
                        <property name="label" translatable="yes">關閉</property>
                        <property name="use_underline">True</property>
                      </object>
                    </child>
                  </object>
                </child>
              </object>
            </child>
            <child>
              <object class="GtkMenuItem" id="menu_basic">
                <property name="visible">True</property>
                <property name="can_focus">False</property>
                <property name="label" translatable="yes">基礎數據</property>
                <property name="use_underline">True</property>
                <child type="submenu">
                  <object class="GtkMenu">
                    <property name="visible">True</property>
                    <property name="can_focus">False</property>
                    <child>
                      <object class="GtkMenuItem" id="menu_basic_company">
                        <property name="visible">True</property>
                        <property name="can_focus">False</property>
                        <property name="label" translatable="yes">公司</property>
                        <property name="use_underline">True</property>
                      </object>
                    </child>
                    <child>
                      <object class="GtkMenuItem" id="menu_basic_supplier">
                        <property name="visible">True</property>
                        <property name="can_focus">False</property>
                        <property name="label" translatable="yes">供應商</property>
                        <property name="use_underline">True</property>
                      </object>
                    </child>
                    <child>
                      <object class="GtkMenuItem" id="menu_basic_supplier_warehouse">
                        <property name="visible">True</property>
                        <property name="can_focus">False</property>
                        <property name="label" translatable="yes">倉庫</property>
                        <property name="use_underline">True</property>
                      </object>
                    </child>
                    <child>
                      <object class="GtkMenuItem" id="menu_basic_shop">
                        <property name="visible">True</property>
                        <property name="can_focus">False</property>
                        <property name="label" translatable="yes">店舖</property>
                        <property name="use_underline">True</property>
                      </object>
                    </child>
                    <child>
                      <object class="GtkMenuItem" id="menu_basic_product2">
                        <property name="visible">True</property>
                        <property name="can_focus">False</property>
                        <property name="label" translatable="yes">商品資料</property>
                        <property name="use_underline">True</property>
                        <child type="submenu">
                          <object class="GtkMenu">
                            <property name="visible">True</property>
                            <property name="can_focus">False</property>
                            <child>
                              <object class="GtkMenuItem" id="menu_basic_product_brand">
                                <property name="visible">True</property>
                                <property name="can_focus">False</property>
                                <property name="label" translatable="yes">品牌</property>
                                <property name="use_underline">True</property>
                              </object>
                            </child>
                            <child>
                              <object class="GtkMenuItem" id="menu_basic_product_category">
                                <property name="visible">True</property>
                                <property name="can_focus">False</property>
                                <property name="label" translatable="yes">分類</property>
                                <property name="use_underline">True</property>
                              </object>
                            </child>
                            <child>
                              <object class="GtkMenuItem" id="menu_basic_product_line">
                                <property name="visible">True</property>
                                <property name="can_focus">False</property>
                                <property name="label" translatable="yes">系列</property>
                                <property name="use_underline">True</property>
                              </object>
                            </child>
                            <child>
                              <object class="GtkMenuItem" id="menu_basic_product_gender">
                                <property name="visible">True</property>
                                <property name="can_focus">False</property>
                                <property name="label" translatable="yes">性別</property>
                                <property name="use_underline">True</property>
                              </object>
                            </child>
                            <child>
                              <object class="GtkMenuItem" id="menu_basic_product_colour">
                                <property name="visible">True</property>
                                <property name="can_focus">False</property>
                                <property name="label" translatable="yes">顏色</property>
                                <property name="use_underline">True</property>
                              </object>
                            </child>
                            <child>
                              <object class="GtkMenuItem" id="menu_basic_product_sizegroup">
                                <property name="visible">True</property>
                                <property name="can_focus">False</property>
                                <property name="label" translatable="yes">尺寸組</property>
                                <property name="use_underline">True</property>
                              </object>
                            </child>
                            <child>
                              <object class="GtkMenuItem" id="menu_basic_product_sizeratio">
                                <property name="visible">True</property>
                                <property name="can_focus">False</property>
                                <property name="label" translatable="yes">尺寸比例</property>
                                <property name="use_underline">True</property>
                              </object>
                            </child>
                            <child>
                              <object class="GtkMenuItem" id="menu_basic_product_userdefineproperties">
                                <property name="visible">True</property>
                                <property name="can_focus">False</property>
                                <property name="label" translatable="yes">自訂屬性</property>
                                <property name="use_underline">True</property>
                              </object>
                            </child>
                          </object>
                        </child>
                      </object>
                    </child>
                    <child>
                      <object class="GtkMenuItem" id="menu_basic_staff">
                        <property name="visible">True</property>
                        <property name="can_focus">False</property>
                        <property name="label" translatable="yes">雇員</property>
                        <property name="use_underline">True</property>
                      </object>
                    </child>
                  </object>
                </child>
              </object>
            </child>
            <child>
              <object class="GtkMenuItem" id="menu_product">
                <property name="visible">True</property>
                <property name="can_focus">False</property>
                <property name="label" translatable="yes">商品</property>
                <property name="use_underline">True</property>
                <child type="submenu">
                  <object class="GtkMenu">
                    <property name="visible">True</property>
                    <property name="can_focus">False</property>
                    <child>
                      <object class="GtkMenuItem" id="menu_product_main">
                        <property name="visible">True</property>
                        <property name="can_focus">False</property>
                        <property name="label" translatable="yes">商品主檔案</property>
                        <property name="use_underline">True</property>
                      </object>
                    </child>
                  </object>
                </child>
              </object>
            </child>
          </object>
          <packing>
            <property name="expand">False</property>
            <property name="fill">True</property>
            <property name="position">0</property>
          </packing>
        </child>
        <child>
          <object class="GtkToolbar">
            <property name="visible">True</property>
            <property name="can_focus">False</property>
          </object>
          <packing>
            <property name="expand">False</property>
            <property name="fill">True</property>
            <property name="position">1</property>
          </packing>
        </child>
        <child>
          <object class="GtkNotebook" id="mainnb">
            <property name="visible">True</property>
            <property name="can_focus">True</property>
          </object>
          <packing>
            <property name="expand">True</property>
            <property name="fill">True</property>
            <property name="position">2</property>
          </packing>
        </child>

1 个答案:

答案 0 :(得分:0)

我的错,我应该使用Gtk::MenuItem::get_submenu()