Results 1 to 4 of 4

Thread: Parsing a CSV File into a QTableView

  1. #1
    Join Date
    Apr 2014
    Posts
    59
    Thanks
    7
    Qt products
    Qt5
    Platforms
    Windows

    Default Parsing a CSV File into a QTableView

    I have this pretty much working and am storing the data in a QList<QString>. When I print it out with qDebug() though, the strings have additional quotes, along with escaped quotes. This makes me think I need to erase the original quotes, when I split the strings?
    Qt Code:
    1. QTextStream bomIn(&bomFile);
    2. QList<QStringList> bomData;
    3. while(!bomIn.atEnd())
    4. {
    5. QString line = bomIn.readLine();
    6. QStringList rowList = line.split(';');
    7. bomData.append(rowList);
    8. }
    9. bomFile.close();
    10.  
    11. // for testing
    12. QString string;
    13. foreach (list, bomData)
    14. {
    15. foreach(string, list)
    16. {
    17. qDebug() << string;
    18. }
    19. }
    20. // end testing
    To copy to clipboard, switch view to plain text mode 
    I also need to display the data in a QTableView, and use one of the columns to run queries in my database. Maybe the quote issue will go away in the view?

    Subclassing QAbstractItemModel looks like the best way to go, but I am having trouble figuring out a couple things. From the examples I have seen, it looks like I need to pass my QList to the model, in order to implement rowCount(), which should be size() - 1, since the first is the headers, I think. I then need a way to set the header data, somehow, the examples were all hard coded. The CSV files could be coming from different sources, and have different headers so that is not an option. I might be able to think of a way to loop through the first QStringList and do it, but am not sure if that is the way to go? I think I figured out the last question I had, while I was writing this.

  2. #2
    Join Date
    Dec 2009
    Location
    New Orleans, Louisiana
    Posts
    791
    Thanks
    13
    Thanked 153 Times in 150 Posts
    Qt products
    Qt5
    Platforms
    MacOS X

    Default Re: Parsing a CSV File into a QTableView

    In my opinion, you should strip the leading and trailing double quotes from your data before storing in your QStringList. Subclassing QAbstractItemModel would allow you to strip the quotes before displaying the data, but why go through that every single time your data needs to be displayed?

    Edit: The QDebug output stream will automatically add quotes to string data as you have observed, so that's another reason you should strip the quotes from your data.
    I write the best type of code possible, code that I want to write, not code that someone tells me to write!

  3. #3
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Parsing a CSV File into a QTableView

    It would be easier for you to store your headers and data in separate string lists. This would be easiest to accomplish when you read it in:

    Qt Code:
    1. QTextStream bomIn(&bomFile);
    2.  
    3. QList<QStringList> bomData;
    4. QStringList headers;
    5. int lineNo = 0;
    6.  
    7. while(!bomIn.atEnd())
    8. {
    9. QString line = bomIn.readLine();
    10. QStringList rowList = line.split(';');
    11.  
    12. if ( lineNo > 0 )
    13. bomData.append(rowList);
    14. else
    15. headers = rowList;
    16. ++lineNo;
    17. }
    18. bomFile.close();
    To copy to clipboard, switch view to plain text mode 

    If you subclass QAbstractItemModel, then you will reimplement the headerData() method to return the appropriate entry from "headers" and data() to return the appropriate entry from the given row in "bomData". rowCount() is bomData.size().
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  4. #4
    Join Date
    Apr 2014
    Posts
    59
    Thanks
    7
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Parsing a CSV File into a QTableView

    I am not sure why I thought the quotes would not be included in the strings, unless it is because I am just used to thinking they should be there, when I add a string. Once I found QStringList::replaceInStrings(), the problem was solved.
    Qt Code:
    1. while(!bomIn.atEnd())
    2. {
    3. QString line = bomIn.readLine();
    4. QStringList rowList = line.split(';');
    5. rowList.replaceInStrings("\"", "");
    6. bomData.append(rowList);
    7. }
    To copy to clipboard, switch view to plain text mode 

    I originally thought to use separate lists, for the headers and data, but that would have meant adding two arguments to the constructor, instead of just one, plus adding another structure to keep track of. I ended up implementing data() like this:
    Qt Code:
    1. QVariant BomTableMode::data(const QModelIndex &index, int role) const
    2. {
    3. if(role == Qt::DisplayRole)
    4. {
    5. QStringList row = rowList.at(index.row() + 1);
    6. return row.at(index.column());
    7. }
    8. return QVariant();
    9. }
    To copy to clipboard, switch view to plain text mode 
    and headerData() like this:
    Qt Code:
    1. QVariant BomTableMode::headerData(int section, Qt::Orientation orientation, int role) const
    2. {
    3. if(role != Qt::DisplayRole)
    4. return QVariant();
    5.  
    6. if(orientation == Qt::Horizontal)
    7. {
    8. QStringList column = rowList.at(0);
    9. return column.at(section);
    10. }
    11.  
    12. return QVariant();
    13. }
    To copy to clipboard, switch view to plain text mode 
    I did similar with rowCount() and columnCount(). I think magic numbers are OK here, since it is easy to understand that the first list is the headers. Except for forgetting to type an "l" at the end of my class name , it seems to be working. Thanks, both of you.

    I did notice at least one of the fields can end up being too long, so I am going to have to implement word wrapping somehow. I assume that is either the view, or the delegate.

Similar Threads

  1. Replies: 3
    Last Post: 4th December 2012, 10:56
  2. QSqlTableModel/ QTableView parsing queries to xml
    By lizi_85 in forum Qt Programming
    Replies: 0
    Last Post: 3rd December 2012, 14:45
  3. Parsing a Qt ts file....
    By PaladinKnight in forum Qt Programming
    Replies: 0
    Last Post: 20th May 2012, 23:01
  4. Parsing .pro file
    By adhit in forum Qt Programming
    Replies: 1
    Last Post: 13th April 2009, 14:23

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.