Home » U++ Library support » TabCtrl » How to dynamically add remove tabs
Re: How to dynamically add remove tabs [message #60185 is a reply to message #60184] |
Wed, 27 September 2023 00:46   |
Oblivion
Messages: 1214 Registered: August 2007
|
Senior Contributor |
|
|
Hello nicesai,
Quote:Question-1: Is it necessary to call the destructor after removing the tab?
tabLay->~WithTabLay();
No, not unless you want to manually destroy the pane object (tablay).
Quote:Question-2: Is it necessary to delete the allocated memory? This line causes the applicatton to crash. So probably I should not, if so, how can I free the allocated memory?
delete tabLay;
Well, if you manually allocate the widget on the heap (something we usually avoid in U++, it is not a good practice), yes you need to delete the object when you're done with it (or it will leak)
Quote:Questions-3: Is there any better way to do this without using pointers? If I create the Tab on stack, its free'ed at the end of the function (from RAII I suppose). And the Tab will not be visible.
In general, if we want to create multiple (a lot of, or indeterminate number of) tabs, we'd usually use containers (e.g. could be Array<WithTabLay<ParentCtrl>>, in this case). Containers take care of mem alloc/dealloc. THAT is the way to go.
An example (just to give you an idea. Not the only way to do it)
#include <CtrlLib/CtrlLib.h>
using namespace Upp;
struct MyApp : TopWindow {
TabCtrl tabs;
Array<Button> panes;
Button bt;
MyApp()
{
Title("Tabs test");
Sizeable().Zoomable().CenterScreen().SetRect(0, 0, 800, 600);
Add(tabs.HSizePosZ().VSizePosZ(0, 28));
Add(bt.SetLabel("Add New Tab").HSizePosZ().BottomPosZ(2, 24));
auto AddPane = [this] {
int n = tabs.GetCount();
Button& b = panes.Add();
b.SetLabel("Pane " + AsString(n) + " (click to delete)");
b << [this] { // DelPane
panes.RemoveIf([this](int i) { return &panes[i] == tabs.GetItem(~tabs).GetSlave(); });
tabs.Remove(~tabs);
};
tabs.Add(b.SizePos(), "Tab " + AsString(n));
};
bt << [&] { AddPane(); };
for(int i = 0; i < 5; i++)
AddPane();
}
};
GUI_APP_MAIN
{
MyApp().Run();
}
Best regards,
Oblivion
Github page: https://github.com/ismail-yilmaz
Bobcat the terminal emulator: https://github.com/ismail-yilmaz/Bobcat
[Updated on: Wed, 27 September 2023 00:51] Report message to a moderator
|
|
|
Goto Forum:
Current Time: Sun Jul 06 19:14:27 CEST 2025
Total time taken to generate the page: 0.02874 seconds
|