PDA

View Full Version : How to access Widgets created in Tab View



kapoorsudhish
22nd October 2009, 12:21
Hi,
I am new to Qt Environment, i am using a QTabWidget where i am adding eight tabs to it in a for loop. the elements present on each page of the tab are same.

I am not able to get the control of widgets present on the 1st tab, can someone please help me on how can i get the control back on the tab one.
Pasting the code snippet.


tabWidget = new QTabWidget(parent);
tabWidget->setGeometry(0,15,650,400);
tabWidget->setStyleSheet("*{background-color:green;}");

for(int i=0; i<8;i++){

QString name(QString::number(1));
tabWidget->addTab(myfunction(),tr("Profile").append(name));
}
connect(Modelcombo,SIGNAL(currentIndexChanged(int) ),this,SLOT(stCurrentIndex(int)));
connect(colorModecombo,SIGNAL(currentIndexChanged( int)),this,SLOT(stCurrentIndex(int)));
}

/*The function is */
QWidget* Widget::myfunction(){

page = new QWidget();
page->setGeometry(0,25,650,500);

QLabel *Modelab = new QLabel(tr("Modle"),page);
Modelab->setGeometry(0,60,120,20);
Modelcombo = new QComboBox(page);
ModelcombosetGeometry(200,60,100,20);
Modelcombo->setFocusPolicy(Qt::NoFocus);
Modelcombo->addItem(tr("Normal"));
Modelcombo->addItem(tr("Bad"));
Modelcombo->addItem(tr("Good"));

QLabel *colorModelab = new QLabel(tr("Color"),page);
colorModelab->setGeometry(0,100,80,20);
colorModecombo = new QComboBox(page);
colorModecombo->setGeometry(200,100,100,20);
colorModecombo->setFocusPolicy(Qt::NoFocus);
colorModecombo->addItem(tr("Red"));
colorModecombo->addItem(tr("Blue"));
colorModecombo->addItem(tr("Green"));

return page;
}
/*the Slot is*/
void Widget::stCurrentIndex(int val){
qDebug()<< "Tab value is" << tabWidget->currentIndex();
qDebug()<< "Modle mode is" << Modelcombo->currentText();
qDebug()<< "Color is" << colorModecombo->currentText();
}

As the content in each tab is the same when i try to connect to signal of combobox in tab 1, the signal is not emitted, but the signal is emitted for the last tab created. Please let me know hw to proceed from here as i want to extract the values of combobox of wach tab.

wysota
23rd October 2009, 00:33
What do you mean by "control"?

kapoorsudhish
23rd October 2009, 06:16
By "control" i mean, i am not able to get the value changed in the combobox of tab 0, if i try
connect(Modelcombo,SIGNAL(currentIndexChanged(int) ),this,SLOT(stCurrentIndex(int)));
connect(colorModecombo,SIGNAL(currentIndexChanged( int)),this,SLOT(stCurrentIndex(int)));

the value shown in the slot is the value of combobox of tab7 and not tab 0, by some way i want the value of the combobox present in each tab individually.
Is the way page created to add to the tab is correct, as all the tabs are having same widgets.

kapoorsudhish
23rd October 2009, 07:48
This code certainly helped as what i was doing initially was incorrect


for(int i=0; i<8;i++)
{
page[i] = new QWidget();

QLabel *recordingModelab[i] = new QLabel(tr("Recording Mode"),page[i]);
recordingModelab[i]->setGeometry(0,60,120,20);
recordingModecombo[i] = new QComboBox(page[i]);
recordingModecombo[i]->setGeometry(200,60,100,20);
recordingModecombo[i]->setFocusPolicy(Qt::NoFocus);
recordingModecombo[i]->addItem(tr("Normal"));
recordingModecombo[i]->addItem(tr("Bad"));
recordingModecombo[i]->addItem(tr("Good"));

QLabel *resolutionModelab[i] = new QLabel(tr("Resolution"),page[i]);
resolutionModelab[i]->setGeometry(0,100,80,20);
resolutionModecombo[i] = new QComboBox(page[i]);
resolutionModecombo[i]->setGeometry(200,100,100,20);
resolutionModecombo[i]->setFocusPolicy(Qt::NoFocus);
resolutionModecombo[i]->addItem(tr("CIF"));
resolutionModecombo[i]->addItem(tr("D1"));
resolutionModecombo[i]->addItem(tr("QCIF"));

QLabel *qualitylab[i] = new QLabel(tr("Bit Rate"),page[i]);
qualitylab[i]->setGeometry(0,140,80,20);
qualitycombo[i] = new QComboBox(page[i]);
qualitycombo[i]->setGeometry(200,140,100,20);
qualitycombo[i]->setFocusPolicy(Qt::NoFocus);
qualitycombo[i]->addItem(tr("50 kbps"));
qualitycombo[i]->addItem(tr("120 kbps"));
qualitycombo[i]->addItem(tr("150 kbps"));
qualitycombo[i]->addItem(tr("200 kbps"));


QLabel *frameratelab[i] = new QLabel(tr("Frame Rate"),page[i]);
frameratelab[i]->setGeometry(0,180,80,20);
frameratecombo[i] = new QComboBox(page[i]);
frameratecombo[i]->setGeometry(200,180,100,20);
frameratecombo[i]->setFocusPolicy(Qt::NoFocus);
frameratecombo[i]->addItem(tr("15 fps"));
frameratecombo[i]->addItem(tr("25 fps"));
frameratecombo[i]->addItem(tr("30 fps"));


QString name(QString::number(1+i));
tabWidget->addTab(page[i],tr("Profile").append(name));

}


If the same can be done in a better way, please share

squidge
23rd October 2009, 08:45
If all the tabs contain the same widgets (and they certainly seem to from your code), then you could create another ui file with your widgets, and then either:

- subclass QWidget using Designers promotion feature and then use the same object for each tab (designer way)
- create 8 copies of the object using 'new' and manually add the tabs using tabWidget->addTab

Either way needs a lot less code than what you have.

kapoorsudhish
23rd October 2009, 13:12
I tried creating 8 copies of the page object, but when i tried to access the values from the comboboxes on tab0, it gave the values of combobox 7 so i tried in this way.