I work in Qt 4.7 and I have a QWidget object in my dialog box. I need to go through each of the children and extract the current text in a QStringList. Every other object is a QCheckBox, and the rest are QComboBoxes (I just need the text part for both). So far, the only way I could do this is to use the children () function to get them as QObject * and throw them, for example:
QStringList textlist;
for(int i = 0; i < ui->myWidget->children().size(); i++)
{
if(i % 2 == 0)
{
QCheckBox *q = (QCheckBox *)ui->myWidget->children().at(i);
textlist.append(q->text());
}
else
{
QComboBox *q = (QComboBox *)ui->myWidget->children().at(i);
textlist.append(q->currentText());
}
}
, , , . , ( QAbstractButton QWidget) QObject, ui- > myWidget- > children(), , , , , . - - , . !
UPDATE: , qobject_cast. , , , QObject QWidget, , QWidget dynamic_cast, . :
QStringList textlist;
for(int i = 0; i < ui->myWidget->children().size(); i++)
{
QWidget *qw = qobject_cast<QWidget*>(ui->myWidget->children().at(i)
if(i % 2 == 0)
{
QComboBox *q = dynamic_cast<QComboBox*>(qw);
if(q)
{
textlist.append(q->text());
}
}
else
{
QCheckBox *q = dynamic_cast<QCheckBox*>(qw);
if(q)
{
textlist.append(q->currentText());
}
}
}
- , . .
UPDATE2: , , , , .. QWidget , . - , , , - , .