Ci3
Ci3

Reputation: 4832

How to retrieve text from a QLineEdit object?

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

Answers (2)

leemes
leemes

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

billz
billz

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

Related Questions