12

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!

jww
  • 97,681
  • 90
  • 411
  • 885
Fracu
  • 835
  • 1
  • 13
  • 28
  • Please place answers in Answer blocks. Later, you can accept your own Answer. Also see [How does accepting an answer work?](https://meta.stackexchange.com/q/5234/173448) – jww Dec 15 '19 at 19:03

4 Answers4

7

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.

Fracu
  • 835
  • 1
  • 13
  • 28
3

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.

ekhumoro
  • 115,249
  • 20
  • 229
  • 336
Arnold Spence
  • 21,942
  • 7
  • 74
  • 67
  • Arnold: Thank you for your answer! but given my basic Qt knowledge I finally did direct assignment. Besides I needed to do more than just hide. Anyway I really appreciate your help. – Fracu Feb 24 '12 at 01:40
  • I figured that hiding wasn't your only goal :) You can put your own answer up and accept it. That will keep it out of search results for unanswered questions. – Arnold Spence Feb 24 '12 at 01:44
  • I just tried, however since I'm less than 100 reputation I have to wait three more hours to do that. Thank you again! – Fracu Feb 24 '12 at 01:57
1

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};
owensss
  • 121
  • 4
0

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

pmr
  • 58,701
  • 10
  • 113
  • 156
tmpearce
  • 12,523
  • 4
  • 42
  • 60