Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

how to create array of Qcombobox



  • i need to create an array of Qcombobox

    i've tried to create different combobox

    comboBox_Origine_1
    comboBox_Origine_2
    comboBox_Origine_3

    now i have to populate the comboboxes with a loop ( i = 0 to 10)

    how can i point to anyone?

    comboBox_Origine_+i->additem("ESTERNA");
    

    this is the errors displayed:

    /Users/matteomissora/EasyGest/inserisciarticolo.cpp:13: error: use of undeclared identifier 'comboBox_Origine_'

    /Users/matteomissora/EasyGest/inserisciarticolo.cpp:13: error: member reference type 'QString' is not a pointer; did you mean to use '.'?

    /Users/matteomissora/EasyGest/inserisciarticolo.cpp:13: error: no member named 'additem' in 'QString'

    i've tried both with

    comboBox_Origine_ & QString::number(i)->additem("ESTERNA");
    

    errors:

    /Users/matteomissora/EasyGest/inserisciarticolo.cpp:14: error: use of undeclared identifier 'comboBox_Origine_'

    /Users/matteomissora/EasyGest/inserisciarticolo.cpp:14: error: member reference type 'int' is not a pointer

    i've to convert "i" index to a pointer??



  • @TheCipo76
    You cannot access a variable name with anything like your comboBox_Origine_+i.

    • If you stick to three distinct variables, you must address them as their full variable name, adjusting your code to reference the right one as appropriate.

    • If instead you create an array/list of comboboxes, they won't have their own variable names, and you can address the desired one via arrayOfCombos[i].

    • Finally you could give each a setObjectName(QString("comboBox_Origine_") + QString.number(i)) (maybe not exact syntax for adding a number to a string, I'm not C++, you can figure that one) and then use "find widget by object name" to address the desired one.



  • ok, i understand that i can't access to combobox as i tried..
    this in my problem..

    i have to populate not 3 but 10 combobox with a loop as i wrote
    this is the reason for which I would want use a cycle..

    i don't find with google search any example how to create and use an array of combobox



  • @TheCipo76
    That's because there isn't anything special to say about "an array of combobox". It's just an array. You know how to handle that, don't you?

    # create 100 comboboxes
    QComboBox *array[100];
    for (int i = 0; i < 100; i++)
    {
        array[i] = new QComboBox;
        layout->addWidget(array[i]);
    }
    
    # access a particular combobox out of the array
    QComboBox *aComboBox = array[50];
    


  • ok i understand..

    thank you very much


  • Lifetime Qt Champion

    Hi,

    Using a QVector<QComboBox *> might be a better idea for easier handling.


Log in to reply