0

I'm trying to insert multiple QComboBoxes from a dynamic multidimensional QComboBox like :

QComboBox **test = new QComboBox *[x];
test[x] = new QComboBox [y];

ui->QVBoxLayout->addWidget(test["one of x values"]["one of y values"]);

But this gives me an error of : no viable convert from QComboBox to *QWidget.

Using :

  QComboBox *test = new QComboBox;
  ui->QVBoxLayout->addWidget(test);

Works just fine.

My case is (this is examplery):

  for(int tmp = 1; fieldAmount >= tmp; tmp++){
            //fieldAmount is the number of fields presented in a table that was loaded in from a file
            combobox1[currentTable] = new QComboBox [tmp];
            ui->verticalLayout_2->addWidget(&combobox1[currentTable][tmp]); //Gives the seg fault

  }

What my case does is based on a file that I load in, finds the amount of tables i will have and in them the amount of values that needs to be entered. That is why I need a dynamic multidimensional QComboBox.

What is the syntax (or execution order) that I'm not getting right ? If this is a duplicate, than im sorry in advance but I was not able to find it the question already posted on here.

Fetert
  • 81
  • 1
  • 6
  • Change `ui->QVBoxLayout->addWidget(test["one of x values"]["one of y values"]);` to `ui->QVBoxLayout->addWidget(&test["one of x values"]["one of y values"]);` – drescherjm Dec 25 '21 at 15:39
  • thank you for point that out, but that does not resolve the main problem. – Fetert Dec 25 '21 at 16:26
  • `QComboBox **test = new QComboBox *[x]; test[x] = new QComboBox [y];` This exhibits undefined behavior by way of accessing an index out of bounds. Valid indexes into `test` are 0 through `x-1`. – Igor Tandetnik Dec 25 '21 at 17:44
  • @Igor Tandetnik you are right but i forgot to mention in my .h is initialize it as `QComboBox **combobox1 = NULL;` So i allocating space for the Comboboxes when I load in a sqlite3 file – Fetert Dec 25 '21 at 17:50
  • The solution to my problem was : `combobox1[currentTable] = new QComboBox [fieldAmount];` `for(int tmp = 0; fieldAmount > tmp; tmp++){` `ui->verticalLayout_2->addWidget(&combobox1[currentTable][tmp]);` `}` – Fetert Dec 25 '21 at 17:51
  • 1
    If you think your solution can be useful to others, post it as an "answer" below (and accept it by pressing the checkmark next to it, to mark the question as solved). – HolyBlackCat Dec 25 '21 at 18:06
  • @HolyBlackCat I will when i find the solution to my problem that this solution gave to me afterward. (Upon inserting them into the LayOut doesn't show the correct amount of QComboBoxes and fails when try to read in the values from them "seg fail".) I'm so lost that I'm considering showing my whole code in order to get help with it. – Fetert Dec 26 '21 at 20:28

1 Answers1

0
combobox1[currentTable] = new QComboBox [fieldAmount];
for(int tmp = 0; fieldAmount > tmp; tmp++){

ui->verticalLayout_2->addWidget(&combobox1[currentTable][tmp]);

}

where fieldAmount is

int fieldAmount = (SQLDataBaseContet->record()).count()-1; // -1 as offset because of id
Fetert
  • 81
  • 1
  • 6