Project

General

Profile

WPushButton with both a connect() and a link

Added by Manu Charlize almost 5 years ago

Hi,

I have a button in my app that I would like for it to be both a link (like an anchor) which will send the user to a new URL, but as well I would like it to call a function to do some cleanup. With my code below, I can only do one or the other. What am I doing wrong?

 std::string link_url{"https://www.google.com"};
 auto button = root()->addWidget(std::make_unique<Wt::WPushButton>(link_url));
 button->setStyleClass("btn-primary");
 button->clicked().connect(this, &MyApp::cleanup);
 button->setLink(Wt::WLink(Wt::LinkType::Url, link_url));

Thanks!


Replies (2)

RE: WPushButton with both a connect() and a link - Added by Roel Standaert almost 5 years ago

If you set a link, clicking the button makes the browser immediately navigate to that link (without a client-server roundtrip). If you connect something else to the clicked() signal it may or may not still send that signal, there's no guarantee.

Similarly, Wt may detect that you are navigating away (an unload event is automatically sent when navigating away), and actually remove the session. You could then use finalize or the destructor of MyApp to do cleanup. If the server does not receive the unload event, then the session is removed after the session timeout.

If you want to send to user to another URL, and also finalize immediately, you could connect the clicked() signal to a function that does a redirect. You can do your cleanup in that same function, or you could call quit() to make sure the WApplication is immediately cleaned up.

RE: WPushButton with both a connect() and a link - Added by Manu Charlize almost 5 years ago

Thanks, that was very helpful!

    (1-2/2)