0

How can I set up my labels to be in an array to be called on later? I can't seem to get a hand on what type labels is in order to pull this off.

What I have is this, however this would be rather ugly to maintain. If I could change this

ui->label_11->setVisible(false);
ui->label_5->setVisible(false);
ui->label_12->setVisible(false);
ui->label_44->setVisible(false);
ui->label_43->setVisible(false);
ui->label_9->setVisible(false);
ui->label_10->setVisible(false);
ui->label_42->setVisible(false);

to this, it would be most preferable. But, perhaps I'm thinking about this in the wrong way.

SometypeIdoNotKnow values[7] = {ui->label_11,    
                                ui->label_5,
                                ui->label_12,
                                ui->label_44,
                                ui->label_43,
                                ui->label_9,
                                ui->label_10,
                                ui->label_42};

for (int i=0; i <= 7; i++){    
    values[i]->setVisible(false);    
}
tisaconundrum
  • 2,156
  • 2
  • 22
  • 37
  • BTW, Your example has UB. I mean `SometypeIdoNotKnow values[7]` then `for (int i=0; i <= 7; i++){ ` – drescherjm Jun 29 '16 at 18:32
  • 2
    How about instead `QList labels = findChildren();` Which will give you a list of all the labels that exist in the parent widget. – drescherjm Jun 29 '16 at 18:35
  • 1
    SometypeIdoNotKnow is a `QLabel*` – drescherjm Jun 29 '16 at 18:36
  • @drescherjm Good idea, but it assumes that the all the labels are to be iterated. – Kuba hasn't forgotten Monica Jun 29 '16 at 20:33
  • I was thinking of that part. If you only need to iterate a subset of that you may still be able to use `findChildren()` perhaps making use `QRegularExpression` parameter (name the labels the same base object name) and / or filter however it depends on your situation. – drescherjm Jun 29 '16 at 20:42

2 Answers2

2

If your compiler is not ancient, use C++11:

// If you only want to iterate some labels
auto const labels = {ui->label_11, ui->label_12, ui->label_44, 
                     ui->label_43, ui->label_9, ui->label_10, 
                     ui->label_42};
// If you want to iterate all labels
auto labels = findChildren<QLabel*>();

for (auto label : labels) label->hide();
Kuba hasn't forgotten Monica
  • 95,931
  • 16
  • 151
  • 313
  • what is up with the auto type? That is really cool how that is working :) Thank you for showing me – tisaconundrum Jun 29 '16 at 22:43
  • also how can i get all the children under a certain parent? Say under `verticalLayout` – tisaconundrum Jun 29 '16 at 22:44
  • @tisaconundrum A layout isn't a parent in the sense of a `QObject` parent. Iteration of widgets managed by a given layout is covered by [this question](http://stackoverflow.com/q/31546511/1329652). – Kuba hasn't forgotten Monica Jun 30 '16 at 16:00
  • 1
    @tisaconundrum "what is up with the auto type" It's not a type. It's an instruction for the compiler to perform type inference. It is a 5+ year old feature of modern C++. You might wish to read up on C++11, it allows your code to be much more readable and concise compared to C++98. – Kuba hasn't forgotten Monica Jun 30 '16 at 16:01
0

Thank you @drescherjm for the answer.
this seems to have worked.

QLabel* values[7] = {ui->label_11, 
                     ui->label_12, 
                     ui->label_44, 
                     ui->label_43, 
                     ui->label_9,  
                     ui->label_10, 
                     ui->label_42};
for (int i = 0; i < 7; i++){       
    values[i]->setVisible(false);  
    qDebug() << i;                 
}  
tisaconundrum
  • 2,156
  • 2
  • 22
  • 37