Error by adding items in a QTableWidget using "for"
Solved
General and Desktop
-
I don't know why but it's not adding any item, here is the code
void BarramDialog::setshunts(QStringList tipo,QStringList nome) { QStringList titulos; titulos <<"Tipo"<<"Nome"; ui->tabela_shunts->setColumnCount(2); ui->tabela_shunts->setHorizontalHeaderLabels(titulos); if(tipo.length()==0){ return; } else { ui->tabela_shunts->setRowCount(tipo.length()); ui->tabela_shunts->setItem(0,0,new QTableWidgetItem(nome[0])); for(int i=0;(i=(tipo.length()-1));i++){ QString type=tipo[i]; QString name=nome[i]; ui->tabela_shunts->setItem(i,0,new QTableWidgetItem(type)); ui->tabela_shunts->setItem(i,1,new QTableWidgetItem(name)); } }
the function receives 2 QStringLists and (should) creates new items in the QTableWidget
(I JUST TESTED WITH ONE ITEM IN EACH LIST SO FAR)
But it doesn't work! not even if do something like this:for(int i=0;(i=(tipo.length()-1));i++){ QString type=tipo[i]; QString name=nome[i]; ui->tabela_shunts->setItem(0,0,new QTableWidgetItem("teste")); ui->tabela_shunts->setItem(0,1,new QTableWidgetItem(name)); }
it just appears the Qtable whit one row, but nothing written inside of the 2 cells
The only way to this code work is if i set the item outside the"for", how can i fix it? -
Hi,
You are using fixed values for both row and column in your for loop. For what you want, you have to update the row value on each iteration.
Note that your loop condition is wrong.
-
Well, use a variable for the row and update it.
Because the condition is what make the loop continue. What you want is "i < count".