PDA

View Full Version : QTabWidget - Add Tabs dynamically



fruzzo
22nd January 2008, 19:15
HI...I have a problem with the dynamically add of tabs to a tabwidget.
I create a GUI with a TabWidget...now, by code, when I add a tab wich has inside a widget created by mine Alarm1 (it has some qlabel and qbuttons inside), the tab was created and added but It don't display anything about the widget Alarm1. Strange because the same code in the constructor works fine like I want. Why?
This is part of the code:


//Add Alarm to Alarms List
tab = new QWidget( twAlarms, "tab" );
tabLayout = new QVBoxLayout( tab, 11, 6, "tabLayout");
Alarm1 * alm = new Alarm1(alarm,tab);
tabLayout->addWidget(alm);
twAlarms->insertTab( tab, QString::fromLatin1("Alarm 1") );
connect(alm, SIGNAL(buttonClicked()), this, SLOT(alarmSent()));

In the constructor instead the code is:


twAlarms->removePage(tab);
tab->close();

for (int i=0; i<3; i++) {
tab = new QWidget( twAlarms, "tab" );
tabLayout = new QVBoxLayout( tab, 11, 6, "tabLayout");
Alarm1 * alm = new Alarm1(alarm, tab);
tabLayout->addWidget(alm);
twAlarms->insertTab( tab, QString::fromLatin1("Alarm"+QString::number(i)) );
connect(alm, SIGNAL(buttonClicked()), this, SLOT(alarmSent()));
}

jpn
22nd January 2008, 19:21
A widget is hidden by default. Tabs created in constructor are shown together with their parent. You must make tabs added dynamically at runtime visible by yourself. Try adding:

tab->show();

fruzzo
23rd January 2008, 08:24
ok I put the line tab->show(); after the connect and so work fine.....now I have a problem with the layout.....the alarm1 widget is printed outside the tab too and not all in the tab, why?

jpn
23rd January 2008, 08:43
Hard to say, it seems to be a custom widget we know nothing about. Since it's properly added to a layout (is it?), it presumably does something that breaks the layout.

fruzzo
23rd January 2008, 10:32
mhmmm...it's strange because I have the same problem if I add in the tab a simble button instead the widget alarm1 :eek:

ashukla
23rd January 2008, 10:36
mhmmm...it's strange because I have the same problem if I add in the tab a simble button instead the widget alarm1 :eek:
Take a look of undo shape example of QtDemo. It's work fine.

fruzzo
23rd January 2008, 10:46
Take a look of undo shape example of QtDemo. It's work fine.

I don't find it...link?

ashukla
23rd January 2008, 11:16
You can see it in your system folder undo folder.

MarkoSan
23rd January 2008, 16:26
And just to comment: I've read in the docs that widget. which will be used, must be previously created WITHOUT parent.

fruzzo
24th January 2008, 08:24
ok I've resolved the problem...it was releated to tab->show() that doesn't work well.
I use instead twAlarms->showPage(...) and now the layout is fine. :p

fruzzo
25th February 2008, 14:06
another problem.... in my QTabWidget I have several tabs that may be of two kinds: tab with inside Alarm1 Widget and tab with inside Alarm2 Widget....Alarm1 and Alarm2 have both a QLabel like an unique identifier of the alarm type.
Now I want to search in the whole QTabWidget which tab containe the Alarm with a particular "identifier"....any idea how to do this?

merlvingian
27th February 2008, 08:26
Use a QHash with either your QLabel or a QString as a lookup index for your alarm widget. There are many examples of similar lookups in QtDemo.


QHash <QLabel *, QWidget *> labelForWidget;