Qt Designer: create an array of widgets

This may be a dumb question, but I'm new to C ++ and Qt. Sorry for the inconvenience.

In Qt Designer, I create several shortcuts (for example):

my_label1
my_label2
my_label3
...
my_label n

Then, if I want to hide them, I do this:

ui->my_label1->hide();
ui->my_label2->hide();
ui->my_label3->hide();
...
ui->my_labeln->hide();

However, I would like to define tags, for example

my_label[n]

So, I could do this:

for(i=0;i<n;i++)
    {
    ui->my_label[n]->hide();
    }

I read that I can define a widget, for example:

QLabel* my_label[5];

but is there a way to do the same with Qt Designer?

Thanks in advance!

Edit:

Finally, I decided to make a direct appointment:

QLabel* my_label_array[5];
my_label_array[0] = ui->my_label1;
my_label_array[1] = ui->my_label2;
my_label_array[2] = ui->my_label3;
my_label_array[3] = ui->my_label4;
my_label_array[4] = ui->my_label5;

Then I can do, for example:

for(idx=0;idx<6;idx++) my_label_array[idx]->show();
for(idx=0;idx<6;idx++) my_label_array[idx]->hide();
for(idx=0;idx<6;idx++) my_label_array[idx]->setEnabled(1);
for(idx=0;idx<6;idx++) my_label_array[idx]->setDisabled(1);
etc...

Then I was able to iterate. I believe this is not the cleanest way to do this, but given my basic knowledge of Qt, I'm fine.

Thanks so much for your answers! This is a great site with great people.

+6
source share
5 answers

Finally, I decided to make a direct appointment:

QLabel* my_label_array[5];
my_label_array[0] = ui->my_label1;
my_label_array[1] = ui->my_label2;
my_label_array[2] = ui->my_label3;
my_label_array[3] = ui->my_label4;
my_label_array[4] = ui->my_label5;

Then I can do, for example:

for(idx=0;idx<6;idx++) my_label_array[idx]->show();
for(idx=0;idx<6;idx++) my_label_array[idx]->hide();
for(idx=0;idx<6;idx++) my_label_array[idx]->setEnabled(1);
for(idx=0;idx<6;idx++) my_label_array[idx]->setDisabled(1);
etc...

. , , , Qt, .

! .

+3

, QObject:: findChildren() , , .

, , , QFrame ( frameShape NoFrame) , setVisible(false) QFrame. , .

, , QStackedWidget.

+3

:

// .hpp
class UiBlabla : public QWidget {
    ...
    QLabel** labels;
};

// constructor
ui->setupUi(this);

labels = new QLabel*[10]{ui->label_0, ui->label_1, ui->label_2, ui->label_3,
                         ui->label_4, ui->label_5, ui->label_6, 
                         ui->label_7, ui->label_8, ui->label_9};
+1

QtDesigner, .

1) my_labelx ( QtDesigner) ( , QVector):

QVector<QLabel*> my_labels;
my_labels.push_back(ui->my_label1);
my_labels.push_back(ui->my_label2);

QVector.

for(int i=0; i < my_labels.size(); ++i) {
   my_labels[i]-> hide();
}
// or with QFOREACH
foreach(QLabel* label, my_labels)
  label->hide();

, QVector, .

2) Depending on the layout of your gui, you can have all your shortcuts as children of the container and iterate over them through the children

0
source

How can I apply this in Python Pyqt5.

QLabel* my_label_array[5];
my_label_array[0] = ui->my_label1;
my_label_array[1] = ui->my_label2;
my_label_array[2] = ui->my_label3;
my_label_array[3] = ui->my_label4;
my_label_array[4] = ui->my_label5;
0
source

All Articles