Qt - How to convert from QObject elements to user interface?

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 , . - , , , - , .

+3
1

qobject_cast. , .

QStringList textlist;
for(int i = 0; i < ui->myWidget->children().size(); i++)
{
    if(i % 2 == 0)
    {
        QCheckBox *q = qobject_cast<QCheckBox*>(ui->myWidget->children().at(i));
        if(q)
           textlist.append(q->text());
    }
    else
    {
        QComboBox *q = qobject_cast<QComboBox*>(ui->myWidget->children().at(i));
        if(q)
           textlist.append(q->currentText());
    }
}

. , , , QStringList. .

mywidget.h:

namespace Ui {
class MyWidget;
}

class MyWidget : public QWidget
{
    Q_OBJECT

public:
    explicit MyWidget(QWidget *parent = 0);
    ~MyWidget();
    QStringList getComboTextList();
    QStringList getCheckBoxTextList();

private:
    Ui::MyWidget *ui;
    QList<QComboBox*> comboList;
    QList<QCheckBox*> checkList;
};

mywidget.cpp:

MyWidget::MyWidget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::MyWidget)
{
    ui->setupUi(this);
    this->setLayout(new QVBoxLayout);
    for(int i = 0; i < 5; i++)
    {
        QCheckBox *checkBox = new QCheckBox(this);
        this->layout()->addWidget(checkBox);
        checkBox->setText(QString("Check box #%1").arg(i));
        checkList.append(checkBox);
    }

    for(int i = 0; i < 5; i++)
    {
        QComboBox *comboBox = new QComboBox(this);
        this->layout()->addWidget(comboBox);
        comboBox->addItem("Combo box item 1");
        comboBox->addItem("Combo box item 2");
        comboList.append(comboBox);
    }
}

MyWidget::~MyWidget()
{
    delete ui;
}

QStringList MyWidget::getComboTextList()
{
    QStringList returnList;
    for(int i = 0; i < comboList.length(); i++)
    {
        returnList << comboList[i]->currentText();
    }
    return returnList;
}

QStringList MyWidget::getCheckBoxTextList()
{
    QStringList returnList;
    for(int i = 0; i < checkList.length(); i++)
    {
        returnList << checkList[i]->text();
    }
    return returnList;
}

getCheckBoxTextList getComboTextList :

QStringList comboTextList = myWidget->getComboBoxList();
QStringList checkTextList = myWidget->getCheckBoxTextList();
+7

All Articles