PDA

View Full Version : Using tableview with CSV



akshaysulakhe
18th July 2013, 14:00
Hello friends,
I am using table view to show some data which will be read from a csv file. But i have to access the csv file through a translator as the contents are going to be changed dynamically, so storage, sync and retrieval should be proper. I have a translator implemented, which uses a hash function. My dilemma is how to dynamically set row count and column count. As i viewed the model view documentation, there are 2 functions and the return value of it decides the row count and column count. I am posting the link for the doc and the function name below :-

http://qt-project.org/doc/qt-4.8/modelview.html
int MyModel::rowCount(const QModelIndex & /*parent*/) const
{
return 2;
}

int MyModel::columnCount(const QModelIndex & /*parent*/) const
{
return 3;
}

As one can see above, if it returns 2 rows, then 2 rows are displayed in the model. How can i do this dynamically with a csv file?

rockdemon
18th July 2013, 14:21
sort've implies that you need to read the csv file, and store the row count. Each time row count is called check if the csv has been updated somehow and re-cache the row count.

ChrisW67
18th July 2013, 22:50
The column count is known once you read the first record of the CSV file.
You can read the entire CSV file to determine a total row count. For a small file this is probably easiest.

Alternatively, implement a lazy fetching mechanism where you read rows in sets of up to n rows and return rowCount() of the number actually read to date. On open/reset the model reads up to n rows and rowCount() returns the number read. Implement canFetchMore() and fetchMore() to allow views to see the entire table if required. QSqlTableModel uses this approach with n == 256.

akshaysulakhe
19th July 2013, 09:28
Thank You guys, the CSV file is not that big. This will work. Ty :-)