How to create Array of objects
-
My custom class for QTableWidgetItem's inherit QTableWidgetItem(obviously), i need to create objects from it somehow. I've made custom class with intention to store there ID of every object and other stuff in future.
I need to create in my main class array of that custom objects, and it's size undefined and elements(objects) should be added in runtime.
If it would be array of Int's or Strings - would be easy, i'm done that simple before, but here is dynamic array of objects.Declaration in header file:
mListItem *Item[];
-
My custom class for QTableWidgetItem's inherit QTableWidgetItem(obviously), i need to create objects from it somehow. I've made custom class with intention to store there ID of every object and other stuff in future.
I need to create in my main class array of that custom objects, and it's size undefined and elements(objects) should be added in runtime.
If it would be array of Int's or Strings - would be easy, i'm done that simple before, but here is dynamic array of objects.Declaration in header file:
mListItem *Item[];
-
My custom class for QTableWidgetItem's inherit QTableWidgetItem(obviously), i need to create objects from it somehow. I've made custom class with intention to store there ID of every object and other stuff in future.
I need to create in my main class array of that custom objects, and it's size undefined and elements(objects) should be added in runtime.
If it would be array of Int's or Strings - would be easy, i'm done that simple before, but here is dynamic array of objects.Declaration in header file:
mListItem *Item[];
@Engelard To add to @Devopia53 take a look at http://doc.qt.io/qt-5/containers.html
Normal C/C++ arrays have fix size by definition. -
Thanks for both of you. I choose(Qt doc. recommend) QVector, but i dont get only one thing, and it does'nt mentioned in documentation. How add new elements to my array?
I mean, i have a function, which should create new item in my QVector, it must create it in element position [1](means second slot), but the size of QVector is [1](by default as i understand). So program compiles well, i run the program, and when i press button - that function(which must create new element in array) try to create it like 10 times, so i'm getting 9 pop-up error dialogs in my screen.
in header:
QVector<mListItem*> itemVal;
In CPP file, inside that function:
itemVal[valID] = new mListItem(); itemVal.at(valID)->setText(QString::number(val)); itemVal.at(valID)->setTextAlignment(Qt::AlignRight); ui->tableWidMemory->setItem(MWindow::tabRows, 1, itemVal.at(valID));
For creating new element i can't use .at() because it is for read only as were said in documentation.
P.S. if someone dont understand, that pop-up critical window errors said that "index is out of range"
-
mListItem* tempItem = new mListItem; tempItem->setText(QString::number(val)); tempItem->setTextAlignment(Qt::AlignRight); itemVal.append(tempItem); ui->tableWidMemory->setItem(MWindow::tabRows, 1, tempItem);
-
@VRonin It's not working, i need not just create object in that function, i need to create it in array..
-
@VRonin It's not working, i need not just create object in that function, i need to create it in array..