You are not logged in.

1

Monday, January 30th 2012, 1:26pm

QComboBox selection in C++ by means its name

Hello. I am working on a GUI cotaining a large number of QComboBox; for many of those the items are added dynamically, during the workflow. In order to simplify the program writing I wold like to add or delete item for a group of QtComboBox, at the same time, which is characterized by some characters of their individual name.
In the documentation I did not find how to select a QtComboBox given its name expressed as QString. As an example, let us suppose that during the workflow I need to modify the QtComboBox named "myQB"; how can operate on it? Please not that i can not use explicit commands like

myQB -> addItem("Donald");

because the name ( "myQB" in the example) of the QtComboBox I need to modify changes during the workflow.

Is there a solution?

2

Monday, January 30th 2012, 1:56pm

maybe objectName helps? - http://developer.qt.nokia.com/doc/qt-4.8…objectName-prop

otherwise you need to keep track of the combox pointers and their names dynamically as your program runs.

and you should be asking yourself why the name of a gui component is changing during the running of the program...
If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

This post has been edited 1 times, last edit by "Amleto" (Jan 30th 2012, 2:12pm)


3

Monday, January 30th 2012, 2:37pm

I apologize for my not clear explanation: the name of the QtComboxBox do not change during the running of the program, but just theirs items may be change.

More precisely my code contains simlar block of code

void ksemaw::PanFit(){
int itab=tabWidget -> currentIndex();
Qt::CheckState state;
state = Qt::Checked;
QString Lj;
if(itab==4){
cBParFit_1 -> clear();
cBParFit_2 -> clear();
cBParFit_3 -> clear();
cBParFit_4 -> clear();
cBParFit_5 -> clear();
cBParFit_6 -> clear();
cBParFit_7 -> clear();
cBParFit_8 -> clear();
cBParFit_9 -> clear();
cBParFit_10 -> clear();
cBParFit_11 -> clear();
cBParFit_12 -> clear();
cBParFit_13 -> clear();
cBParFit_14 -> clear();
cBParFit_15 -> clear();
cBParFit_16 -> clear();
cBParFit_17 -> clear();
for(int i=1;i<=9;i++){
Lj="L"+QString::number(i);
if(i==1) state=cBL1 -> checkState();
if(i==2) state=cBL2 -> checkState();
if(i==3) state=cBL3 -> checkState();
if(i==4) state=cBL4 -> checkState();
if(i==5) state=cBL5 -> checkState();
if(i==6) state=cBL6 -> checkState();
if(i==7) state=cBL7 -> checkState();
if(i==8) state=cBL8 -> checkState();
if(i==9) state=cBL9 -> checkState();
if( state == Qt::Checked ) {
cBParFit_1-> addItem(Lj+"_d");
cBParFit_1-> addItem(Lj+"_Dn/<n>");
cBParFit_1-> addItem(Lj+"_(nav/<n>-1)");
cBParFit_1-> addItem(Lj+"_Dk/<k>");
cBParFit_1-> addItem(Lj+"_(kav/<k>-1)");
cBParFit_1-> addItem(Lj+"_sigma_roughness");
cBParFit_2-> addItem(Lj+"_d");
cBParFit_2-> addItem(Lj+"_Dn/<n>");
cBParFit_2-> addItem(Lj+"1_(nav/<n>-1)");
cBParFit_2-> addItem(Lj+"_Dk/<k>");
cBParFit_2-> addItem(Lj+"_(kav/<k>-1)");
cBParFit_2-> addItem(Lj+"_sigma_roughness");
cBParFit_3-> addItem(Lj+"_d");
cBParFit_3-> addItem(Lj+"_Dn/<n>");
cBParFit_3-> addItem(Lj+"_(nav/<n>-1)");
cBParFit_3-> addItem(Lj+"_Dk/<k>");
cBParFit_3-> addItem(Lj+"_(kav/<k>-1)");
cBParFit_3-> addItem(Lj+"_sigma_roughness");
cBParFit_4-> addItem(Lj+"_d");
cBParFit_4-> addItem(Lj+"_Dn/<n>");
cBParFit_4-> addItem(Lj+"_(nav/<n>-1)");
cBParFit_4-> addItem(Lj+"_Dk/<k>");
cBParFit_4-> addItem(Lj+"_(kav/<k>-1)");
cBParFit_4-> addItem(Lj+"_sigma_roughness");
cBParFit_5-> addItem(Lj+"_d");
cBParFit_5-> addItem(Lj+"_Dn/<n>");
cBParFit_5-> addItem(Lj+"_(nav/<n>-1)");
cBParFit_5-> addItem(Lj+"_Dk/<k>");
cBParFit_5-> addItem(Lj+"_(kav/<k>-1)");
cBParFit_5-> addItem(Lj+"_sigma_roughness");
cBParFit_6-> addItem(Lj+"_d");
cBParFit_6-> addItem(Lj+"_Dn/<n>");
cBParFit_6-> addItem(Lj+"_(nav/<n>-1)");
cBParFit_6-> addItem(Lj+"_Dk/<k>");
cBParFit_6-> addItem(Lj+"_(kav/<k>-1)");
cBParFit_6-> addItem(Lj+"_sigma_roughness");
cBParFit_7-> addItem(Lj+"_d");
cBParFit_7-> addItem(Lj+"_Dn/<n>");
cBParFit_7-> addItem(Lj+"_(nav/<n>-1)");
cBParFit_7-> addItem(Lj+"_Dk/<k>");
cBParFit_7-> addItem(Lj+"_(kav/<k>-1)");
cBParFit_7-> addItem(Lj+"_sigma_roughness");
cBParFit_8-> addItem(Lj+"_d");
cBParFit_8-> addItem(Lj+"_Dn/<n>");
cBParFit_8-> addItem(Lj+"_(nav/<n>-1)");
cBParFit_8-> addItem(Lj+"_Dk/<k>");
cBParFit_8-> addItem(Lj+"_(kav/<k>-1)");
cBParFit_8-> addItem(Lj+"_sigma_roughness");
cBParFit_9-> addItem(Lj+"_d");
cBParFit_9-> addItem(Lj+"_Dn/<n>");
cBParFit_9-> addItem(Lj+"_(nav/<n>-1)");
cBParFit_9-> addItem(Lj+"_Dk/<k>");
cBParFit_9-> addItem(Lj+"_(kav/<k>-1)");
cBParFit_9-> addItem(Lj+"_sigma_roughness");
cBParFit_10-> addItem(Lj+"_d");
cBParFit_10-> addItem(Lj+"_Dn/<n>");
cBParFit_10-> addItem(Lj+"_(nav/<n>-1)");
cBParFit_10-> addItem(Lj+"_Dk/<k>");
cBParFit_10-> addItem(Lj+"_(kav/<k>-1)");
cBParFit_10-> addItem(Lj+"_sigma_roughness");
cBParFit_11-> addItem(Lj+"_d");
cBParFit_11-> addItem(Lj+"_Dn/<n>");
cBParFit_11-> addItem(Lj+"_(nav/<n>-1)");
cBParFit_11-> addItem(Lj+"_Dk/<k>");
cBParFit_11-> addItem(Lj+"_(kav/<k>-1)");
cBParFit_11-> addItem(Lj+"_sigma_roughness");
cBParFit_12-> addItem(Lj+"_d");
cBParFit_12-> addItem(Lj+"_Dn/<n>");
cBParFit_12-> addItem(Lj+"_(nav/<n>-1)");
cBParFit_12-> addItem(Lj+"_Dk/<k>");
cBParFit_12-> addItem(Lj+"_(kav/<k>-1)");
cBParFit_12-> addItem(Lj+"_sigma_roughness");
cBParFit_13-> addItem(Lj+"_d");
cBParFit_13-> addItem(Lj+"_Dn/<n>");
cBParFit_13-> addItem(Lj+"_(nav/<n>-1)");
cBParFit_13-> addItem(Lj+"_Dk/<k>");
cBParFit_13-> addItem(Lj+"_(kav/<k>-1)");
cBParFit_13-> addItem(Lj+"_sigma_roughness");
cBParFit_14-> addItem(Lj+"_d");
cBParFit_14-> addItem(Lj+"_Dn/<n>");
cBParFit_14-> addItem(Lj+"_(nav/<n>-1)");
cBParFit_14-> addItem(Lj+"_Dk/<k>");
cBParFit_14-> addItem(Lj+"_(kav/<k>-1)");
cBParFit_14-> addItem(Lj+"_sigma_roughness");
cBParFit_15-> addItem(Lj+"_d");
cBParFit_15-> addItem(Lj+"_Dn/<n>");
cBParFit_15-> addItem(Lj+"_(nav/<n>-1)");
cBParFit_15-> addItem(Lj+"_Dk/<k>");
cBParFit_15-> addItem(Lj+"_(kav/<k>-1)");
cBParFit_15-> addItem(Lj+"_sigma_roughness");
cBParFit_16-> addItem(Lj+"_d");
cBParFit_16-> addItem(Lj+"_Dn/<n>");
cBParFit_16-> addItem(Lj+"_(nav/<n>-1)");
cBParFit_16-> addItem(Lj+"_Dk/<k>");
cBParFit_16-> addItem(Lj+"_(kav/<k>-1)");
cBParFit_16-> addItem(Lj+"_sigma_roughness");
cBParFit_17-> addItem(Lj+"_d");
cBParFit_17-> addItem(Lj+"_Dn/<n>");
cBParFit_17-> addItem(Lj+"_(nav/<n>-1)");
cBParFit_17-> addItem(Lj+"_Dk/<k>");
cBParFit_17-> addItem(Lj+"_(kav/<k>-1)");
cBParFit_17-> addItem(Lj+"_sigma_roughness");
}
}
for(int i=1;i<=20;i++){
Lj="O"+QString::number(i);
if(i==1) state=cBosc_1 -> checkState();
if(i==2) state=cBosc_2 -> checkState();
if(i==3) state=cBosc_3 -> checkState();
if(i==4) state=cBosc_4 -> checkState();
if(i==5) state=cBosc_5 -> checkState();
if(i==6) state=cBosc_6 -> checkState();
if(i==7) state=cBosc_7 -> checkState();
if(i==8) state=cBosc_8 -> checkState();
if(i==9) state=cBosc_9 -> checkState();
if(i==10) state=cBosc_10 -> checkState();
if(i==11) state=cBosc_11 -> checkState();
if(i==12) state=cBosc_12 -> checkState();
if(i==13) state=cBosc_13 -> checkState();
if(i==14) state=cBosc_14 -> checkState();
if(i==15) state=cBosc_15 -> checkState();
if(i==16) state=cBosc_16 -> checkState();
if(i==17) state=cBosc_17 -> checkState();
if(i==18) state=cBosc_18 -> checkState();
if(i==19) state=cBosc_19 -> checkState();
if(i==20) state=cBosc_20 -> checkState();
if( state == Qt::Checked ) {
cBParFit_1-> addItem(Lj+"_C");
cBParFit_1-> addItem(Lj+"_E");
cBParFit_1-> addItem(Lj+"_D");
cBParFit_1-> addItem(Lj+"_K");
cBParFit_2-> addItem(Lj+"_C");
cBParFit_2-> addItem(Lj+"_E");
cBParFit_2-> addItem(Lj+"_D");
cBParFit_2-> addItem(Lj+"_K");
cBParFit_3-> addItem(Lj+"_C");
cBParFit_3-> addItem(Lj+"_E");
cBParFit_3-> addItem(Lj+"_D");
cBParFit_3-> addItem(Lj+"_K");
cBParFit_4-> addItem(Lj+"_C");
cBParFit_4-> addItem(Lj+"_E");
cBParFit_4-> addItem(Lj+"_D");
cBParFit_4-> addItem(Lj+"_K");
cBParFit_5-> addItem(Lj+"_C");
cBParFit_5-> addItem(Lj+"_E");
cBParFit_5-> addItem(Lj+"_D");
cBParFit_5-> addItem(Lj+"_K");
cBParFit_6-> addItem(Lj+"_C");
cBParFit_6-> addItem(Lj+"_E");
cBParFit_6-> addItem(Lj+"_D");
cBParFit_6-> addItem(Lj+"_K");
cBParFit_7-> addItem(Lj+"_C");
cBParFit_7-> addItem(Lj+"_E");
cBParFit_7-> addItem(Lj+"_D");
cBParFit_7-> addItem(Lj+"_K");
cBParFit_8-> addItem(Lj+"_C");
cBParFit_8-> addItem(Lj+"_E");
cBParFit_8-> addItem(Lj+"_D");
cBParFit_8-> addItem(Lj+"_K");
cBParFit_9-> addItem(Lj+"_C");
cBParFit_9-> addItem(Lj+"_E");
cBParFit_9-> addItem(Lj+"_D");
cBParFit_9-> addItem(Lj+"_K");
cBParFit_10-> addItem(Lj+"_C");
cBParFit_10-> addItem(Lj+"_E");
cBParFit_10-> addItem(Lj+"_D");
cBParFit_10-> addItem(Lj+"_K");
cBParFit_11-> addItem(Lj+"_C");
cBParFit_11-> addItem(Lj+"_E");
cBParFit_11-> addItem(Lj+"_D");
cBParFit_11-> addItem(Lj+"_K");
cBParFit_12-> addItem(Lj+"_C");
cBParFit_12-> addItem(Lj+"_E");
cBParFit_12-> addItem(Lj+"_D");
cBParFit_12-> addItem(Lj+"_K");
cBParFit_13-> addItem(Lj+"_C");
cBParFit_13-> addItem(Lj+"_E");
cBParFit_13-> addItem(Lj+"_D");
cBParFit_13-> addItem(Lj+"_K");
cBParFit_14-> addItem(Lj+"_C");
cBParFit_14-> addItem(Lj+"_E");
cBParFit_14-> addItem(Lj+"_D");
cBParFit_14-> addItem(Lj+"_K");
cBParFit_15-> addItem(Lj+"_C");
cBParFit_15-> addItem(Lj+"_E");
cBParFit_15-> addItem(Lj+"_D");
cBParFit_15-> addItem(Lj+"_K");
cBParFit_16-> addItem(Lj+"_C");
cBParFit_16-> addItem(Lj+"_E");
cBParFit_16-> addItem(Lj+"_D");
cBParFit_16-> addItem(Lj+"_K");
cBParFit_17-> addItem(Lj+"_C");
cBParFit_17-> addItem(Lj+"_E");
cBParFit_17-> addItem(Lj+"_D");
cBParFit_17-> addItem(Lj+"_K");
}
}
}
}


You see, the most of instructions would be syntetized with a "for" if I could act on the combo by means of its name, like "cBParFit-"+Qstring.number(i)

4

Monday, January 30th 2012, 5:21pm

oh wow! that is some ugly code!

yes, you can do things easily by using a dictionary/map.

QMap<QString, QComboBox*> idToComboBox;

idToComboBox["one"] = new QComboBox();
idToComboBox["two"] = new QComboBox();

idToComboBox["one"]->addItem("one--one");
idToComboBox["two"]->addItem("two--one");

etc etc.

you can loop over all the pairs in the map in a few ways, here is one:

Source code

1
2
3
4
5
 QMap<QString, int>::const_iterator i = map.constBegin();
 while (i != map.constEnd()) {
     cout << i.key() << ": " << i.value() << endl;
     ++i;
 }

http://developer.qt.nokia.com/doc/qt-4.8/qmap.html
If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

5

Tuesday, January 31st 2012, 9:46am

Many many thanks!!!!
Now my code is much more clean:

void ksemaw::PanFit(){
int itab=tabWidget -> currentIndex();
Qt::CheckState state;
state = Qt::Checked;
QString Lj;
if(itab==4){
for(int j=1;j<=17;j++){
idToComboBox["cBParFit_"+QString::number(j)] -> clear();
}
for(int i=1;i<=9;i++){
Lj="L"+QString::number(i);
state=idToCheckBox["cBL"+QString::number(i)]-> checkState();
if( state == Qt::Checked ) {
for(int j=1;j<=17;j++){
for(int k=1;k<=6;k++){
idToComboBox["cBParFit_"+QString::number(j)]-> addItem(Lj+ParFitLab[k]);
}
}
}
}
for(int i=1;i<=20;i++){
Lj="O"+QString::number(i);
state=idToCheckBox["cBosc_"+QString::number(i)]-> checkState();
if( state == Qt::Checked ) {
for(int j=1;j<=17;j++){
for(int k=7;k<=10;k++){
idToComboBox["cBParFit_"+QString::number(j)]-> addItem(Lj+ParFitLab[k]);
}
}
}
}
}
}

6

Tuesday, January 31st 2012, 1:49pm

Althought the suggested solution makes easier the code writing, the initial assignation (i.e. idToComboBox["one"] = new QComboBox();) is quite cumbersome.
Why not introduce the general possibilty to invoque any Qxxxx by its name, expressed as QString?

7

Tuesday, January 31st 2012, 4:06pm

after seeing the code, factories/reflection felt a bit too advanced ;)
If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

8

Tuesday, January 31st 2012, 5:58pm

I apologize, I did not want to be presumptuous. The question I asked was innocent. Is there no way to invoke any QWidget by the name assigned to it?

9

Tuesday, January 31st 2012, 6:57pm

depends what you mean by the name assigned by it, and what you mean by invoke. It looked as though you were confusing variable name with the objectname of a qobject.

e.g. QWidget mywidget; does not mean the QWidget is called "mywidget".

Also, you invoke methods, not classes. So it doesn't make sense to say you invoke a QWidget.

If you want to make a QWidget using string "QWidget", then yes there is a way already in Qt:
http://developer.qt.nokia.com/doc/qt-4.8…tml#newInstance

You can also use a standard factory - simple example here http://login2win.blogspot.com/2008/05/c-factory-pattern.html

If you have already made many QWidgets, then you cannot use some string to obtain a single instance.
If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

10

Wednesday, February 1st 2012, 8:47am

The links you indicated seem much more sophisticated than my problem is. Let me resume: I arranged a GUI by Qt 4 Designer; it contains hunderds of combobox, checkbox, lineedit, doublespinbox etc.; I did not create new QWidget class, I only use those offered by the Qt 4 Designer (you know, I am just a beginner). My rpoblem is to find a simpel method to initially set values/options of the GUI and then read the new choice to be send to the program driven by the GUI. Concerning the example-code I submitted above, I have to set the items of numerous of combobox. The simplest way I know is, as an example,

QComboBox_pippo ->addItem("pluto");

Let us suppose i have to repeat the same operation on tenths of QComboBox, for istance

QComboBox_pippo_1 ->addItem("pluto");
QComboBox_pippo _2->addItem("pluto");
..
QComboBox_pippo_N ->addItem("pluto");

if I could write something like

QtComboBox(name="QComboBox_pippo_1") -> addItem("pluto");

I could sinthetize with

for(i=1;i<=N;i++) QtComboBox(name="QComboBox_pippo_"+QString::number(i)) -> addItem("pluto");

The QMap solution you indicated is in this way, but it requires the initial assignation

idToComboBox["QComboBox_pippo_1"]=QComboBox_pippo_1;

that is once again quite cumebrsome.

In conclusion I just searching for a way to act on the QComboBox_pippo by means of its objecName "QComboBox_pippo".
I hope my problem is now more clear..

Many thanks in advance for your help and suggestions!

11

Wednesday, February 1st 2012, 11:00am

the name that you give to it in designer is just the VARIABLE name that is auto generated in the ui->Setup() piece of code. That variable name is not in any way associated with a string value that you can use as an identifier to any widget (necessarily).

What you have is a problem joining the auto generated code to some abstraction.

You can solve your problem by not using the form designer and desiging the forms manually in your own code.

e.g.

Source code

1
2
3
4
5
6
7
8
9
10
11
QStringList names;
QMap<QString, QWidget*> map;
names << "one" << "two" << three;

QVBoxLayout layout = new QVBoxLayout();
foreach(QString s, names)
{
  QComboBox* ptr = new QComboBox();
  layout->addWidget(ptr);
  names[s] = ptr
}

Now you have your own layout that you can add to any widget, all you had to do was use a list of the names.
If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

This post has been edited 3 times, last edit by "Amleto" (Feb 1st 2012, 11:18am)