9
votes

In Qt Designer I'm creating multiple labels (for instance):

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 the labels like

my_label[n]

So then I would be able to do this:

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

I read that I can define the widget like:

QLabel* my_label[5];

but is there any way to do the same from Qt Designer?

Thanks in advance!

4
Please place answers in Answer blocks. Later, you can accept your own Answer. Also see How does accepting an answer work? - jww

4 Answers

5
votes

Finally I decided to do direct assignment:

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 instance:

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 perform iterations. I believe is not the cleanest way to do it but given my basic knowledge of Qt is ok for me.

Thank you very much for your answers and your support! This is a great site with great people.

3
votes

Instead of creating an explicit array, you may be able to name your widgets using a particular scheme and then use QObject::findChildren() on the parent widget to get a list of the widgets you are after.

If you only want to hide widgets, you can put all the widgets you want to hide in an invisible QFrame (set frameShape to NoFrame) and hide them all by calling setVisible(false) on the QFrame. This may cause some unwanted side effects with layouts so you may have to tweak some size policy settings.

In case you are wanting to hide controls so that you can simulate a wizard type UI, you may want to check into QStackedWidget.

1
votes

I have another dirty workaround for this:

in header file

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

in source file

// 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};
0
votes

I haven't seen anything in QtDesigner to do that, but there are a couple of relatively easy ways to get that behavior.

1) Simply store the my_labelx pointers (from QtDesigner) in an array (or better, a QVector):

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

Then you can iterate through the QVector.

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

There is a little setup needed in terms of adding all the labels to the QVector, but on the plus side you only do that once.

2) Depending on the layout of your gui, you could have all your labels be children of a container object and iterate through the children