msrinivasan32 Posted March 4, 2019 Share Posted March 4, 2019 Hello, I have a GUI that is created in AppWorldLogic and the GuiPtr for it is established as public in the corresponding header file. For my project, I also have another class (say, MyClass) defined that needs to be able to add and manipulate widgets on the same GUI, so I have tried to pass in the GuiPtr from AppWorldLogic into the constructor for MyClass. However, when I subsequently try to add children to the GUI from the MyClass's constructor, they never appear. Any suggestions on what I've done wrong or what else I could try to troubleshoot this issue? Thanks! Link to comment
silent Posted March 5, 2019 Share Posted March 5, 2019 msrinivasan32 Could you please send us your source code? It would be much easier to suggest a solution if we can check the sources. Thanks! How to submit a good bug report --- FTP server for test scenes and user uploads: ftp://files.unigine.com user: upload password: 6xYkd6vLYWjpW6SN Link to comment
msrinivasan32 Posted March 6, 2019 Author Share Posted March 6, 2019 Yes here is some snippets of the code, without the other irrelevant chunks: // Inside AppWorldLogic.h class AppWorldLogic : public Unigine::WorldLogic { public: virtual int init(); Unigine::GuiPtr gui_ptr; bool initGUI(); private: MyClass class_obj; }; // Inside AppWorldLogic.cpp int AppWorldLogic::init() { initGUI(); return 1; } bool AppWorldLogic::initGUI() { gui_ptr = Gui::get(); class_obj = MyClass(gui_ptr); gui_ptr->setEnabled(1); return true; } // Inside MyClass.h class MyClass { private: Unigine::WidgetButtonPtr myButton; public: MyClass(); MyClass(Unigine::GuiPtr gui_ptr); }; // Inside MyClass.cpp MyClass::MyClass() { } MyClass::MyClass(Unigine::GuiPtr gui_ptr) { Unigine::WidgetButtonPtr myButton = Unigine::WidgetButton::create(gui_ptr, "Button"); gui_ptr->addChild(myButton->getWidget(), Unigine::Gui::ALIGN_OVERLAP); } This does not work and when I debugged the issue, it seems the gui pointer is actually fine, but creating the WidgetButton in MyClass is the issue. When I instead put the following line of code inside initGUI( ) in AppWorldLogic.cpp and then pass the button pointer into the constructor of MyClass, everything works and appears fine. Unigine::WidgetButtonPtr myButton = Unigine::WidgetButton::create(gui_ptr, "Button"); Is there a reason why this button creation does not work inside MyClass but it does work in AppWorldLogic? Also is there something I can do to allow widgets to be created and added to the Gui from other cpp files – not AppWorldLogic? Thanks! Link to comment
andrey-kozlov Posted March 6, 2019 Share Posted March 6, 2019 Hello msrinivasan32, Unigine::WidgetButtonPtr myButton = Unigine::WidgetButton::create(gui_ptr, "Button"); myButton destroys the button in a destructor when the scope is ended, that's a common pitfall. Lifetime need to be extended. This one should work (as MyClass already has myButton member): myButton = Unigine::WidgetButton::create(gui_ptr, "Button"); Link to comment
Greg.Mildenhall Posted March 6, 2019 Share Posted March 6, 2019 Without looking into this in any depth, it sounds like it's getting garbage collected. You might need to keep a reference to the Widget in MyClass. I won't speculate why it doesn't get GCed the other way. Link to comment
msrinivasan32 Posted March 6, 2019 Author Share Posted March 6, 2019 Andrey-kozlov, that resolved my issue. Thank you. Link to comment
Recommended Posts