Accessing widgets which are members of a GridLayout

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";
connect(countrycombo, SIGNAL(currentIndexChanged(int)), this, SLOT(countryselected(int)));

//Suppose there 8 more widgets here        


//Here is the body part

QLabel *label0 = new QLabel;
QLabel *label1 = new QLabel;


//suppose there are 10 labels here and 8 more of the commands below.


for (int i=1;i<9;i++){

        QComboBox *combo1 = new QComboBox;
        QSpinBox *spin1 = new QSpinBox;
        QSpinBox *spin2 = new QSpinBox;
        QSpinBox *spin3 = new QSpinBox;



//Bring them all together            

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:

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());

Need Your Help

Doclava cannot find symbol SystemProperties (Android javadoc)

android javadoc

I am running doclava (javadoc with the Android-style doclava doclet) on an existing codebase. At this stage I'm using javadoc via a simple command line, so this is not an Ant or Eclipse issue. I've...

entity framework complex objects with lists

entity-framework object code-first complextype

this is my first question on stack overflow so i'll try to be precise.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.