0

I have a qtableview, some of its columns' data are like: [20,1647.8,2101.2,1911.6, ... ,-1.6915] It's basically an array of float values separated by comma and wrapped up by square brackets. But the first one is an integer telling how many values in this array.

I need to get every value separated by ",", so first I use QModelIndex to get the cell's content, then I convert the data type from QVariant to QString, followed by other processing:

QModelIndex index = myTable->model()->index(row,col,QModelIndex());
QVariant arrayQVar = myTable->model()->data(index);
QString arrayStr = arrayQVar.toString();  
//.... Other processing follows, the above repeated many times

Sometimes, I would say sth like 1/10000 times arrayStr is different from arrayQVar

For example, When I print out arrayQVar I got sth like:

QVariant(QString, "[20,1647.8,2101.2,1911.6,756.84...]")

But if I print out arrayStr, I got sth like:

[20,-4388.4,-6596.3,-6368.5,...]

All values are different except the first one.

I am using Qt 5.15.10 MinGW 64-bit, the above codes run in a different thread other than GUI thread.

This happens randomly, i.e. A random cell in the table can have this issue, and it doesn't always happen. I think QVariant to QString conversion is a very basic conversion...right? maybe I need to change the first value to 20.0? Since the difference takes place after the first number...

  • Where and how are you printing your variables? Since you're using threads, could there be a race condition where the value changes in the middle of your operation? – JarMan Sep 22 '22 at 01:48
  • @JarMan Ok, looks like I found the issue: In the new thread I set a table pointer (myTable) pointing to the QTableView created in the GUI thread. QTableView data model is QSqlQueryModel fetching data from DataBase (All the above happen in GUI thread). Then in the new thread when I try to fetch data through myTable pointer error happens... So later I tried getting DataBase data into data model directly in the new thread everything becomes fine. Honestly, this is weird to me (I am new to QThread) – SuperNoooob Sep 23 '22 at 03:56
  • If I find the reason of "error in multithreading data model" I will modify my question, thank you very much – SuperNoooob Sep 23 '22 at 04:01

0 Answers0