Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create widget array using Qt Designer?

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!

like image 332
Fracu Avatar asked Feb 23 '12 22:02

Fracu


People also ask

How do I create a custom widget in Qt?

Adding the Custom Widget to Qt Designer. Click Tools|Custom|Edit Custom Widgets to invoke the Edit Custom Widgets dialog. Click New Widget so that we are ready to add our new widget. Change the Class name from 'MyCustomWidget' to 'Vcr'.

What is .UI file in Qt?

ui file is used to create a ui_calculatorform. h file that can be used by any file listed in the SOURCES declaration. Note: You can use Qt Creator to create the Calculator Form project. It automatically generates the main.

What is Qt widgets?

Widgets are the primary elements for creating user interfaces in Qt. Widgets can display data and status information, receive user input, and provide a container for other widgets that should be grouped together. A widget that is not embedded in a parent widget is called a window.


1 Answers

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.

like image 148
Fracu Avatar answered Oct 02 '22 08:10

Fracu