Project

General

Profile

Creating popupmenu with WTemplate

Added by nourieh sadat zabetzadeh about 4 years ago

I want to create a popupmenu with WTemplat in qt. as you know with WTemplate Object in Wt you can get style to objects in CSS file and don't need to use addStyleClass funtion. In follow codes i create a navigation that have a menuitem insid of it's. in the other side i create popupmenu but when i want to set this popupmenu to menuitem i face to a broblem that how can do that? please attention that i want do this just by WTemplate.

XML codes:

<messages>
   <message id="2th_level_logo_template">
      ${POPUP_MENUE_SECTION class="logo_left w3-hide-small w3-bar-item w3-button fa"}
   </message>
   <message id="3th_level_logo_template">
      ${ITEM_POPUP_MENUE class="logo_left"}
   </message>
   <message id="w3_Template">
     ${NAVIGATION_SECTION class="w3-top w3-bar w3-white w3-card"}
   </message>
</messages>

wt codes:

 std::unique_ptr<WTemplate> w3_t = make_unique<WTemplate>(WString::tr("w3_Template"));
    std::unique_ptr<WNavigationBar> nav_section = std::make_unique<WNavigationBar>();
    std::unique_ptr<WTemplate> w3_popup_menu_item_logo = make_unique<WTemplate>(WString::tr("2th_level_logo_template"));
    std::unique_ptr<WMenuItem> popup_menu_item = std::make_unique<WMenuItem>(WString::tr("language_nvg"));
    popup_menu_item->setIcon("popup_down_arrow.png");
    std::unique_ptr<WTemplate> w3_popup_menu_logo = make_unique<WTemplate>(WString::tr("3th_level_logo_template"));
    std::unique_ptr<WPopupMenu> popup_menu = std::make_unique<WPopupMenu>();    
    popup_menu->addItem(WString::tr("language_prs"));
    popup_menu->addSeparator();
    popup_menu->addItem(WString::tr("language_eng"));
    w3_popup_menu_logo->bindWidget("ITEM_POPUP_MENUE", move(popup_menu));
    popup_menu_item->setMenu(std::unique_ptr<WPopupMenu>(dynamic_cast<WPopupMenu*>(w3_popup_menu_logo->resolveWidget("ITEM_POPUP_MENUE"))));
    w3_popup_menu_item_logo->bindWidget("POPUP_MENUE_SECTION", move(popup_menu_item));
    nav_section->addWidget(move(w3_popup_menu_item_logo));
    w3_t->bindWidget("NAVIGATION_SECTION", move(nav_section));

Replies (1)

RE: Creating popupmenu with WTemplate - Added by Roel Standaert about 4 years ago

This line looks quite hairy to me:

popup_menu_item->setMenu(std::unique_ptr<WPopupMenu>(dynamic_cast<WPopupMenu*>(w3_popup_menu_logo->resolveWidget("ITEM_POPUP_MENUE"))));

resolveWidget returns a raw (non-owning!) pointer, and its being turned into a unique_ptr, which causes the popup menu to have two owners: the popup_menu_item, and w2_popup_menu_logo.

Also, on this line:

w3_popup_menu_item_logo->bindWidget("POPUP_MENUE_SECTION", move(popup_menu_item));

You're binding a WMenuItem directly to a WTemplate. A menu item is really just intended to be a child of a WMenu.

There seems to be no reason for the existence of w3_popup_menu_logo.

I'm not sure what the exact HTML is that you want to get, but I think it probably won't be possible to do what you want entirely using WTemplates, if you want to make use of WMenu, MenuItem, and WPopupMenu. I would say: add a WMenu to WNavigationBar with WNavigationBar::setMenu. Add a WMenuItem to said WMenu, and then add a WPopupMenu to that WMenuItem with WMenuItem::setMenu.

I think the only part you'll be able to specify in your XML is the w3_Template part. All of the other style classes, you will just have to set with addStyleClass.

    (1-1/1)