[SOLVED] Program crashes when loading data from a .bin file to QTableView when a button is pressed
-
Hi,
You set the row and column count but that doesn't create the item in there.
Have a look at the "QTableWidget's documentation":http://qt-project.org/doc/qt-4.8/qtablewidget.html#details
-
First of all thank you for your reply. I'm reading the documentation but it doesn't click.
bq. Items are created ouside the table (with no parent widget) and inserted into the table with setItem():
Can you give me another clue?
FYI,
I'm using a QTableView not a QTableWidget -
Sorry I misread, but I think the answer is still relevant, you modify the row & column count of the model and try to access the items without creating them first.
-
bq. you modify the row & column count of the model and try to access the items without creating them
Sorry but I don't get this, the items were created here by streaming data from the file, no?
@stream >> n >> m;@Sorry if I don't see the obvious but this is the first time I use MV.
-
When you write the item to the file you are writing the data and the flags of the item.
When you want to read, you have to provide an item to read these data and flags.
@model->item(i,j)@
points to a null item
-
[quote author="fs_tigre" date="1375973884"]bq. you modify the row & column count of the model and try to access the items without creating them
Sorry but I don't get this, the items were created here by streaming data from the file, no?
@stream >> n >> m;@Sorry if I don't see the obvious but this is the first time I use MV.[/quote]
What SGaist is referring to is that you are assuming in your load function that the model is already completely initialized/populated.
Let's assume you have a model of size 10 rows and 20 cols. If you write those and load them again without destroying model, it will work. If you load a smaller model, let's say 8 rows and 12 cols, it will work.
If you load a larger model (or when have not a model initialized already) you will fail. Let's assume 12 rows and 20 cols, when you had previously a 10 by 20 model. Where should row 11 and 12 come from?
You use item(i,j) but you did not assign memory in the section you are showing us. Certainly you may do this somewhere else, but is creating in general a problem.
[edit, was a bit slow, but basically the same answer as SGaist, but giving you also the hint, why it does work sometimes]
-
Ok, make sense. I basically added a for loop to create empty rows and it works fine, but for some reason I have a filling that this can be optimized.
Is this how you would do it?
Load Funtion:
@QFile file("/Users/UserName/Practicing/Resource_Files/someFile.bin"); if (file.open(QIODevice::ReadOnly)) { QDataStream stream(&file); qint32 n, m; stream >> n >> m; // new for loop starts here for (int e=0; e<n; ++e) { for (int e=0; e<m; e++) { QStandardItem *item1 = new QStandardItem(""); QStandardItem *item2 = new QStandardItem(""); QList<QStandardItem*> row; row <<item1 << item2; model->appendRow(row); } } // new for loop ends here model->setRowCount(n); model->setColumnCount(m); for (int i=0; i<n; ++i) { for (int j=0; j<m; j++) { model->item(i,j)->read(stream); } } file.close(); }@
-
Something like that ?
@
QDataStream stream(&file);
qint32 row, col;
stream >> row >> col;
model->setRowCount(row);
model->setColumnCount(col);// new for loop starts here for (int i = 0; i < row ; ++i) { for (int j = 0; j < col; j++) { QStandardItem *item = new QStandardItem; item->read(stream); model->setItem(i, j, item); } }
@
-
I knew that the way I did it wasn't the best way. Thanks a lot for your help!
-
You're welcome !
If everything if fine now, can you update the thread's title to solved ? So other forum users may know a solution has been found :)