Unknown behavior of WPopupMenu
Hi
The main class of the application is as follows:
class Raha : public Wt::WApplication
{
public:
Wt::Signal<int, int> sizeChanged_;
Wt::JSignal<int, int> jSizeChanged_;
Raha::Raha(const Wt::WEnvironment &env) :
Wt::WApplication(env),
jSizeChanged_(this, "jSizeChanged_")
{
jSizeChanged_.connect ([=](int w, int h){
sizeChanged_.emit(w, h);
});
doJavaScript("window.addEventListener('resize',function() {" +
jSizeChanged_.createCall({"Math.max(document.documentElement.clientWidth || 0, window.innerWidth || 0)", "Math.max(document.documentElement.clientHeight || 0, window.innerHeight || 0)"}) + "});" +
jSizeChanged_.createCall({"Math.max(document.documentElement.clientWidth || 0, window.innerWidth || 0)", "Math.max(document.documentElement.clientHeight || 0, window.innerHeight || 0)"}));
root()->addWidget(std::make_unique<Bar>());
}
~Raha()
{
}
};
I have used a JSignal and the doJavaScript() function to detect the size of the page, and I have used a Signal to distribute the resize event throughout the application.
In another part of the program, a Wt::WPopupMenu and submenu have been created as follows:
class Bar : public Wt::WContainerWidget
{
public:
Bar(){
auto app = dynamic_cast<Raha*>(Wt::WApplication::instance());
app->sizeChanged_.connect ([=](int w, int h){createBar (w);});
}
void createBar (int w){
clear();
auto menu = std::make_unique<Wt::WPopupMenu>();
menu->addItem ("item1");
menu->addItem ("item2");
auto subMenu = std::make_unique<Wt::WPopupMenu>();
subMenu->addItem ("subItem1");
subMenu->addItem ("subItem2");
menu->addMenu ("subMenu", std::move (subMenu));
menu->addItem ("item3");
auto btn = addWidget (std::make_unique<Wt::WPushButton>("Menu"));
btn->clicked().connect (menu.get(), &Wt::WPopupMenu::popup);
addWidget (std::move (menu));
}
};
Opening and closing the main menu is fine. The submenu opens and closes correctly the first time, but the second time it opens, it doesn't close at all until the page is refreshed (while the main menu closes). When creating a simple code to reproduce this problem, I found that using the Bar::createBar() method as a Slot causes this problem, but using it as follows fixes the problem:
Raha::Raha(const Wt::WEnvironment &env) :
Wt::WApplication(env),
jSizeChanged_(this, "jSizeChanged_")
{
.
.
.
auto bar = root()->addWidget(std::make_unique<Bar>());
bar->createBar (375);
}
The attached file contains the complete program.
The program is compiled by Wt4.12.1