In a function I am currently working on, I am creating a multi-dimensional array of checkboxes, with the dimensions specified at run-time by the user. In order to represent the 'z' dimension, I create multiple tabs -- each one representing a different dimension -- and create an array of check boxes in each tab. The tabs are labeled dim1, dim2, dim3, ... etc.
The problem I am having is the fact that in order to create the array of check boxes (within 3 'for' loops), I have to call the tabs as follows:
checkBoxVector.append(new QCheckBox( ui->dim1 ));
Where checkBoxVector holds pointers to the checkboxes. Now my first thought was that I would simply create a variable name that would change with each loop. With each iteration, it would go: "dim1", then "dim2", "dim3", ... etc. The problem with this is that I cann开发者_StackOverflowot reference the tabs with a string variable, I must type in the actual name of the tab. Here is a sample of that code:
int num = k+1;
QString dim = "dim";
QString tab_name = dim.append(QString("%1").arg(num));
checkBoxVector.append(new QCheckBox( ui->tab_name ));
This gives me the error " 'class Ui::MainWindow' has no member named 'tab_name' ".
Therefore; my question is: how can I apply this idea of changing the NAME of the tab with each loop, without causing such an error?
EDIT: I think I forgot to mention that the tabs have already been created at this point, and have already been labeled with the "dim1", "dim2", "dim3", ... names. The only issue I am having is how to reference these tabs after they have been created. I feel like there is a simple syntax solution.
Store the pointers to tabs in an array and index it with the variable that you use for iterating over the third dimension of your multidimensional array of checkboxes, your code would look something like this:
QTabWidget* tabs[3];
tabs[0] = ui->dim1;
tabs[1] = ui->dim2;
tabs[2] = ui->dim3;
// ... and then
checkBoxVector.append(new QCheckBox( tabs[z] ));
Someone will say it better (there is a concept name for that) but you can't dynamically declare members in c++ ... if you want QString tab_name
content to be in any way related to a member of ui
in this part of code:
QString tab_name = dim.append(QString("%1").arg(num));
checkBoxVector.append(new QCheckBox( ui->tab_name ));
The compiler need to know at compile time witch member of ui
it will use to find the parent of the the new QCheckBox
.
Correct me if I am wrong, you have dimz
tabs and dimy*dimx
QcheckBox
in each tabs. You don't know any dim*
when you build you ui
file ?
So create your own widget (QWidget
) it will store an array of QcheckBox*
that you can dynamically create in constructor for example. This widget will take care of the proper layout of the check boxes.
Then finally you create you own QTabWidget
that will create dimz
tabs of your widget. You will access your tabs with their indexes corresponding to their z
coordinate.
You're trying to reference a member variable that does not exist:
int num = k+1;
QString dim = "dim";
QString tab_name = dim.append(QString("%1").arg(num));
checkBoxVector.append(new QCheckBox( ui->tab_name ));
The error is caused by the fact that you are passing a non-existing member variable to the constructor of QCheckBox. The object the ui pointer is pointing to has no member variable named tab_name. If I understood your description correctly, just pass the local variable tab_name to the QCheckBox constructor like this:
checkBoxVector.append(new QCheckBox( tab_name ));
精彩评论