Who controls WPopupMenu?
Added by Steve Forman about 6 years ago
I have some Widget, that is often constructed and destructed. This is some code, like:
button = new WPushButton;
button->setToolTip(toWString(QObject::tr("")));
button->addStyleClass("dropdown-button");
addWidget(button);
popup = new WPopupMenu;
popup->addStyleClass("dropdown-menu");
button->setMenu(popup);
popup->itemSelected().connect(this, &DropdownMenu::onItemChanged);
So, when i call setMenu, WPushButton is now responsble for deletion of WPopupMenu or i need to delete menu by myself?
Replies (3)
RE: Who controls WPopupMenu? - Added by lm at about 6 years ago
Wt 4 constitutes a redesign of this aspect of Wt to make these kinds of things clearer. Consider updating.
According to Wt 4 documentation, Wt::WPushButton::setMenu
takes ownership: https://www.webtoolkit.eu/wt/doc/reference/html/classWt_1_1WPushButton.html#ae919cb76993211553b50a996bdf80fe9 so I would assume the same is the case in Wt 3.
RE: Who controls WPopupMenu? - Added by Steve Forman about 6 years ago
I have played a little bit with WPopupMenu and it is not deleted, when WPushButton is deleted.
Code:
WPushButton *btn = new WPushButton;
btn->setText("test");
WPopupMenu *popup = new WPopupMenu;
popup->addItem("item1");
qWarning() << popup->itemAt(0)->text().toUTF8().c_str();
btn->setMenu(popup);
qWarning() << popup;
delete btn;
qWarning() << popup;
qWarning() << popup->itemAt(0)->text().toUTF8().c_str();
My Wt version - 3.3.1. Maybe in Wt4 its different
RE: Who controls WPopupMenu? - Added by Roel Standaert about 6 years ago
I just had a look. This is what I found:
In Wt 3, the WPopupMenu
will be a direct child widget of the "domRoot" (that's the parent of WApplication::root()
), so if the WApplication
is deleted, the WPopupMenu
is also deleted. You can call delete
on it yourself, and that will do the right thing, though. There's no risk of double free there, since the WPopupMenu
will automatically remove itself from its parent.
In Wt 4 it's indeed a lot more clear: WPushButton::setMenu()
takes a unique_ptr
, so that means ownership of the WPopupMenu
is transferred to the button, which makes a lot more sense in my opinion. I guess that's one of the many minor fix-ups of Wt 4.