Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Update: Forum Guidelines & Code of Conduct


    Qt World Summit: Early-Bird Tickets

    Solved how to create array of Qcombobox

    General and Desktop
    3
    6
    1227
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • TheCipo76
      TheCipo76 last edited by TheCipo76

      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??

      JonB 1 Reply Last reply Reply Quote 0
      • JonB
        JonB @TheCipo76 last edited by JonB

        @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.

        1 Reply Last reply Reply Quote 5
        • TheCipo76
          TheCipo76 last edited by

          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

          JonB 1 Reply Last reply Reply Quote 0
          • JonB
            JonB @TheCipo76 last edited by

            @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];
            
            1 Reply Last reply Reply Quote 1
            • TheCipo76
              TheCipo76 last edited by

              ok i understand..

              thank you very much

              1 Reply Last reply Reply Quote 0
              • SGaist
                SGaist Lifetime Qt Champion last edited by

                Hi,

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

                Interested in AI ? www.idiap.ch
                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                1 Reply Last reply Reply Quote 4
                • First post
                  Last post