I am trying to make a model using QStandardItemModel
in Qt. I have done it in the following two ways. In the second method I just expanded the loop, and I get redefinition of 'item'
error, while the first method works fine.
method 1
QStandardItemModel * model = new QStandardItemModel( 3, 1 );
QStringList modelStepsTags = {"item-1","item-2","item-3"};
for( int r=0; r<3; r=r+1 )
{
QStandardItem *item = new QStandardItem( modelStepsTags.at(r) );
model->setItem(r, 0, item);
}
method 2
QStandardItemModel * model = new QStandardItemModel( 3, 1 );
QStringList modelStepsTags = {"item-1","item-2","item-3"};
QStandardItem *item = new QStandardItem( modelStepsTags.at(0) );
model->setItem(0, 0, item);
QStandardItem *item = new QStandardItem( modelStepsTags.at(1) );
model->setItem(1, 0, item);
QStandardItem *item = new QStandardItem( modelStepsTags.at(2) );
model->setItem(2, 0, item);
Item is defined three times.
QStandardItemModel * model = new QStandardItemModel( 3, 1 );
QStringList modelStepsTags = {"item-1","item-2","item-3"};
QStandardItem *item = new QStandardItem( modelStepsTags.at(0) ); // here
model->setItem(0, 0, item);
QStandardItem *item = new QStandardItem( modelStepsTags.at(1) ); // here
model->setItem(1, 0, item);
QStandardItem *item = new QStandardItem( modelStepsTags.at(2) ); // and here
model->setItem(2, 0, item);
You can get away with reusing the first definition
QStandardItemModel * model = new QStandardItemModel( 3, 1 );
QStringList modelStepsTags = {"item-1","item-2","item-3"};
QStandardItem *item = new QStandardItem( modelStepsTags.at(0) );
model->setItem(0, 0, item);
item = new QStandardItem( modelStepsTags.at(1) ); // reuse
model->setItem(1, 0, item);
item = new QStandardItem( modelStepsTags.at(2) ); // reuse
model->setItem(2, 0, item);
Or discard the variable entirely as it is redundant
QStandardItemModel * model = new QStandardItemModel( 3, 1 );
QStringList modelStepsTags = {"item-1","item-2","item-3"};
model->setItem(0, 0, new QStandardItem( modelStepsTags.at(0) ));
model->setItem(1, 0, new QStandardItem( modelStepsTags.at(1) ));
model->setItem(2, 0, new QStandardItem( modelStepsTags.at(2) ));