I am a newbie Qt Developer (almost a month) and I searched the net for a solution on my problem but I found nothing. Maybe, I didn't know how or what to ask about it. So, I will present the problem that haunts me a couple of days now.
I create a form dynamically with the following commands:
QWidget *window = new QWidget;
QGridLayout *headerlayout = new QGridLayout;
QGridLayout *bodylayout = new QGridLayout;
QGridLayout *layout = new QGridLayout;
QLabel *countrylabel = new QLabel;
QComboBox *countrycombo = new QComboBox;
country << "" << "England" << "Germany" << "Greece" << "Italy" << "Netherlands";
countrycombo->addItems(country);
countrylabel->setText("Χώρα");
connect(countrycombo, SIGNAL(currentIndexChanged(int)), this, SLOT(countryselected(int)));
//Suppose there 8 more widgets here
headerlayout->addWidget(countrylabel,0,0);
headerlayout->addWidget(countrycombo,0,1);
//Here is the body part
QLabel *label0 = new QLabel;
QLabel *label1 = new QLabel;
label0->setText("LABEL1");
label1->setText("<b>LABEL2</b>");
//suppose there are 10 labels here and 8 more of the commands below.
bodylayout->addWidget(label0,0,0);
bodylayout->addWidget(label1,0,1);
//HERE IS the CLEVER PART
for (int i=1;i<9;i++){
QComboBox *combo1 = new QComboBox;
QSpinBox *spin1 = new QSpinBox;
QSpinBox *spin2 = new QSpinBox;
QSpinBox *spin3 = new QSpinBox;
bodylayout->addWidget(combo1,i,0);
bodylayout->addWidget(spin1,i,1);
bodylayout->addWidget(spin2,i,2);
bodylayout->addWidget(spin3,i,3);
}
//END OF CLEVER PART
//Bring them all together
layout->addLayout(headerlayout,1,10,0);
layout->addLayout(bodylayout,10,10,0);
window->setLayout(layout);
With this code I create a beautiful dynamic form, with nothing to worry about. MY problem is described by the following question:
-How I can access the widgets in the CLEVER part in order to change or read their properties (ex. current index for Combo Boxes, values for Spinboxes etc). What I want to do is to make a selection to the countrycombo (on the top), then change the values of a second combo depending on the first combo value (not described here, its in the headerlayout) and after that, change the values at the combo boxes within the CLEVER part and "read" the values from the spinboxes in order to make some SQL "magic".
I also read about SIGNALs and SLOTs, but the problem stands. The objectname or address within the CLEVER part.
I read about creating a QList, but I don't know if it is a good idea.
Thanks Nik
You can access any QObject instance using the following code:
QObject *object = parent->findChild<Class*>(objectName)
So all you have to do in your code is to define an object name when creating your widgets. When you place it to your QGridLayout it becomes a parent for them, so you can do something like this:
countryCombo->setObjectName("Country");
...
QComboBox *combo = headerLayout->findChild<QComboBox*>("Country");
Or you can use a widget that own's your layout - no matter.
The other option is when you using slots, that are executed when widget emits a signal. If you want to access the widget-sender you can use the following code:
QComboBox *combo = qobject_cast<QComboBox*>(QObject::sender());