Reputation: 4832
I have an array of pointers to QLineEdit objects, and I would like to iterate through them and output the text that they hold. It seems like I'm having trouble with pointers..
QList<QLineEdit *> boxes = ui->gridLayoutWidget->findChildren<QLineEdit *>();
for(QList<QLineEdit *>::iterator it = boxes.begin(); it != boxes.end(); it++)
{
qDebug() << **it->text(); //not sure how to make this correct
}
I can output the object and name with qDebug, so I know the findChildren() and iterator is set up alright, but I'm not sure how to get the text.
Upvotes: 0
Views: 820
Reputation: 45665
Why are you using iterators? Qt has the nice foreach
loop which does this stuff for you and simplifies the syntax:
QList<QLineEdit *> boxes = ui->gridLayoutWidget->findChildren<QLineEdit *>();
foreach(QLineEdit *box, boxes) {
qDebug() << box->text();
}
Upvotes: 1
Reputation: 45410
Try:
for(QList<QLineEdit *>::iterator it = boxes.begin(); it != boxes.end(); it++)
{
qDebug() << (*it)->text();
}
It's the same as below code, just save one intermediate pointer:
for(QList<QLineEdit *>::iterator it = boxes.begin(); it != boxes.end(); it++)
{
QlineEdit* p= *it; // dereference iterator, get the stored element.
qDebug() << p->text();
}
operator->
has higher precedence than operator*
, see C++ operator wiki
Upvotes: 1