Quirin
Quirin

Reputation: 13

Changing pointer in a loop

I'm on C++ and the QT IDE: I want to save the user input of different boxes to an array. I can read the value of the inputbox by this command: ui->h8x->value()

My boxes are numbered like this: h1x, h2x, .... h16x How can I change the h[i]x pointer in a for loop, is it possible?

    for(i=0; i<16; i++)
     {
      array[i]=ui->h[i]x->value();
     }

Upvotes: 1

Views: 280

Answers (1)

masoud
masoud

Reputation: 56539

h[i]x is not a C++ valid syntax.

You should first insert all Qt widgets into a QVector, then index them. For example:

QVector<QLineEdit *> h;

h.append(ui->h1x);
h.append(ui->h2x);
.
.
h.append(ui->h16x);

then you can have this:

for(int i=0; i<16; i++)
{
  array[i] = h[i]->text();
}

Upvotes: 5

Related Questions