Widgets not displaying as expected from container

Added by Alex Gouws 3 months ago

I have a main class with a container that holds some widgets and a class object that represents a different page. When I click on the button it loads the other page from the WStackedWidget but the widgets don't look as I expect them to. Some of them don't even seem to show. I've attached some screenshots. The code is below. In main.cpp, the comments represent the widgets in the WStackedWidget index. (0=Wtext, 1=WPushButton, etc). When I set containerStack->setCurrentIndex(3); then the widgets from the class object display correctly (they all display). If I change the index to 1 and click the button, the page changes, but it doesn't show all the widgets from the class object.

main.cpp

class HelloApplication : public Wt::WApplication
{
public:
    HelloApplication(const Wt::WEnvironment& env);

private:
    WStackedWidget *contain;
    void changePage();
};

void HelloApplication::changePage()
{
    Wt::WApplication *app = Wt::WApplication::instance();
    if (app->internalPath() == "/dashboard") {
        contain->setCurrentIndex(3);
    }
}

HelloApplication::HelloApplication(const Wt::WEnvironment& env)    : Wt::WApplication(env)
{   
    Wt::WApplication *app = Wt::WApplication::instance();
    app->internalPathChanged().connect([=] {
        if (app->internalPath() == "/dashboard") {
            contain->setCurrentIndex(3);
        }
    });

    setTitle("Hello world");

    //set bootstrap v3 theme
    auto theme = std::make_shared<Wt::WBootstrapTheme>();
    theme->setVersion(Wt::BootstrapVersion::v3);
    wApp->setTheme(theme);

    auto containerStack{ cpp14::make_unique<Wt::WStackedWidget>() };
    contain = containerStack.get();    //get pointer so that container can be accessed outside of constructor
    Wt::WAnimation animation(Wt::AnimationEffect::Fade, Wt::TimingFunction::Linear, 200);
    containerStack->setTransitionAnimation(animation, true);

    WText *text{ containerStack->addNew<WText>("Hello World!") };    //0
    WPushButton *button{ containerStack->addNew<WPushButton>("Click me!") };    //1
    button->setStyleClass("btn-primary");
    button->clicked().connect([=] {
        app->setInternalPath("/dashboard", true);
    });

    auto panel = Wt::cpp14::make_unique<Wt::WPanel>();
    panel->addStyleClass("centered-example");
    panel->setTitle("Terrific panel");
    panel->setCentralWidget(Wt::cpp14::make_unique<Wt::WText>("This is a panel with a title."));

    containerStack->addWidget(std::move(panel));    //2
    dashboard *dash{ containerStack->addNew<dashboard>() };    //3

    containerStack->setCurrentIndex(1);
    root()->addWidget(std::move(containerStack));
}

dashboard.h

class dashboard    : public WContainerWidget
{
public:
    dashboard();
    WPushButton *button;
    WText *text;
    WGroupBox *groupBox;
    WPanel *panel;

    ~dashboard();
};

dashboard.cpp
dashboard::dashboard()
{
    button = this->addWidget(cpp14::make_unique<WPushButton>("Dashboard button"));
    text = this->addWidget(cpp14::make_unique<WText>("This is your dashboard."));

    groupBox = this->addWidget(cpp14::make_unique<Wt::WGroupBox>("A group box"));
    groupBox->addStyleClass("centered-example");
    groupBox->addWidget(Wt::cpp14::make_unique<Wt::WText>("<p>Some contents.</p>"));
    groupBox->addWidget(Wt::cpp14::make_unique<Wt::WText>("<p>More contents.</p>"));

    panel = this->addWidget(cpp14::make_unique<Wt::WPanel>());
    panel->addStyleClass("centered-example");
    panel->setTitle("Terrific panel");
    panel->setCentralWidget(std::unique_ptr<WPushButton>(button));
}

Why do the widgets not display?


Replies (4)

RE: Widgets not displaying as expected from container - Added by lm at 3 months ago

Hmm, it does seem that you're doing it wrong, but my question is not "Why don't the widgets show?" but rather "Why do the widgets show?"!

Alex Gouws wrote:

I have a main class with a container that holds some widgets and a class object that represents a different page. ... When I set containerStack->setCurrentIndex(3); then the widgets from the class object display correctly (they all display).

This is where I'm thrown off. I don't know why they would all show. This seems strange to me.

Obviously it's allowed by the type system, but I think you should only put Wt::WContainerWidget (or derivatives) into the Wt::WStackedWidget. I'm curious enough that I'm trying to reproduce here.

RE: Widgets not displaying as expected from container - Added by Alex Gouws 3 months ago

Why would it seem strange that they all show?

When the stackContainer index is accessed which has an instance of my class which itself contains widgets in a container (being itself, as it's derived from a WContainerWidget) then those widgets are simply displayed on the screen. But what doesn't make sense is that when I change it here: containerStack->setCurrentIndex(3); (with my class as the active index) all the widgets are displayed. But when I change it with contain->setCurrentIndex(3); in the changePage function it doesn't show the widgets like it did before, but it's still accessing the same widget container (my class) is it not?

RE: Widgets not displaying as expected from container - Added by lm at 3 months ago

Sorry, I'm not able to compile Wt applications at the moment. It looks like my own-compiled Wt libraries are relying on boost 1.68 which I don't have; I have boost 1.69. Sorry!

(1-4/4)