Results 1 to 4 of 4

Thread: How can i load/display csv file in QTableWidget?(long read, sorry)

  1. #1
    Join Date
    Mar 2010
    Posts
    8
    Thanks
    7
    Qt products
    Qt4

    Default How can i load/display csv file in QTableWidget?(long read, sorry)

    So what i am trying to do is make my app that has spreadsheet(qtablewidget) as mainwindows central widget open and display csv files in such a way that every word in csv file loaded by my app(all words in csv file are separated by comma) is displayed in QTableWidget cell in natural flow so on the end my spreadsheet would display each word from csv file in its own cell in spreadsheet by natural flow as it looks in csvreaders.



    Currently i am studying QT Programming book (ver 2) and i must say i am having a rought time, i am rereading, memorizing, trying to figure this book and even its sometimes really a pain i am not giving up.

    What did i learn so far following the spreadsheet example is how to write file from my spreadsheet(qtablewidget) and how to open that file.

    The logic that program follows for doing this is simply - Iterate over each row and column, specify each column, get the data from it as text and give that text to Qstring for writting in a file and with row_colun iteration write row, column and its string to a file with QDataStream in a sequence so on the end file written ends like

    row 1 - column 1 - string, row 1 -column 2 - string, row 1 - column 3 - string and so on... all the way to the end of last column in table


    Now to read this and display it on widget book example is using while loop and puts (!in.atEnd()) as condition so if input streaming is not done it will display each cell that is written inside file on my spreadsheet widget.

    It does so because each row and column is written as int number inside the file, it takes these numbers in order starting with(row 1 - column 1) passes these as arguments to a function in my class that accordingly writes all text(string) in row and column it gets and on the end bam every text is in every cell written and my table is written, phew this reminded me how much headaches i had before simply figuring how this works. Maybe i am dumb or this book is just not suited for starters but i am learning alot.

    So, why am i writting this on the end. Because i cant figure the logic that QDataStream uses. I see how i can read my own Qtable spreadsheet files because each time i write data with QDataStream i will write inside it a row, column, text and use my function to display this data in table because it gets required parameters for it but HOW IN THE WORLD...

    am i going to make my application load some .csv that lets say i download from the Web to open it and display it, how if i have no clue what is inside it? (the low level data) because i am assuming when some csv program saves csv file and some guy emails(example) that file to me it surely doesnt have same low level data(like my spreadsheet files do) that i can simply pass it to my app and its functions for displaying(loading), so what am i gonna do then, is it possible to somehow achieve this?

    Sorry for long read but i tried to be specific so you can understand my situation better, thank you for reading
    Last edited by msimurin; 15th March 2010 at 04:05.

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: How can i load/display csv file in QTableWidget?(long read, sorry)

    A CSV file is basic text and the correct Qt tool to read or write it would be QTextStream with some parsing logic to split lines at the delimiters. QDataStream, as used by the book's Spreadsheet example, is a binary format reader/writer that's more suited to making program data structures persistent on disk in a portable fashion.

    You might also like to look at Qxt, which has a CSV file model.

  3. #3
    Join Date
    Feb 2010
    Posts
    53
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How can i load/display csv file in QTableWidget?(long read, sorry)

    I'm having the same problem msimurin.

    This code not for csv works but for ordinary txt files works slightly in that something is imported but not correctly

    Qt Code:
    1. {
    2.  
    3.  
    4. clear();
    5. QTextStream in(&file);
    6. QString txt;
    7. const int TabSize =8;
    8. int endlCount =0;
    9. int spaceCount=0;
    10. int column=0;
    11.  
    12. QApplication::setOverrideCursor(Qt::WaitCursor);
    13. while (!in.atEnd())
    14. {
    15. in >> txt;
    16. if (txt=='\n') // row
    17. {
    18. //++endlCount;
    19. //spaceCount=0;
    20. //column=0;
    21. ++row;
    22. }
    23. else if (txt=='\t') //column
    24. {
    25. //int size=TabSize-(column%TabSize);
    26. //spaceCount+=size;
    27. //column+=size;
    28. ++column;
    29. }
    30. else if (txt==' ') //column
    31. {
    32. //++spaceCount;
    33. ++column;
    34. }
    35. /*else
    36. {
    37.  
    38.  
    39. //row=endlCount;
    40.  
    41.   }*/
    42. // ++column;
    43. in >> row >> column >> txt;
    44. setFormula(row, column, txt); //spreadsheet specific code given in C++ GUI programming with QT4
    45. }
    46. }
    To copy to clipboard, switch view to plain text mode 

    The code given in the above book for importing txt files doesn't compile it doesn't like something about the join() function used.

    I also found this code posting somewhere which imports CSV files as one row yuo might find useful

    Qt Code:
    1. Note:
    2. tbl_trades is my own subclassed QTableWidget.
    3.  
    4.  
    5. void mywidget::read_line()
    6. {
    7. char* cfg_file = getenv("FILE");
    8. QFile file( cfg_file );
    9.  
    10. if ( file.open( QIODevice::ReadOnly | QIODevice::Text ) ) {
    11. while ( !file.atEnd() ) {
    12. QByteArray line = file.readLine();
    13. QStringList vals = QStringList::split( ',', line, 0 );
    14. if ( vals.size() == no_of_cols ) { // valid line of text
    15. tbl_trades->insertRow( 0 ); // new row
    16. tbl_trades->setRowHeight( 0, 20 ); // resize new row
    17. for ( int i = 0; i < no_of_cols; i++ ) { //
    18. vals[i] );
    19. tbl_trades->setItem( 0, i, temp );
    20. }
    21. }
    22. }
    23. }
    24. }
    To copy to clipboard, switch view to plain text mode 

    I haven't given up on this I think the answer is to stream it in

    count delimiters so for txt (how does csv give columns?)

    1) count rows using split( '\n') and put this in stringlist
    2) then count columns
    3) then iterate over rows and column count and use split( '\t') into cells using the tab delimiter

    however my attempt to do this has failed in that it seems to put something in the cell 0,0 but not anything visible and nothing in the other cells trying to import a txt file with

    one three
    two four

    Qt Code:
    1. QTextStream in(&file);
    2. QString txt;
    3. QStringList columns;
    4. QApplication::setOverrideCursor(Qt::WaitCursor);
    5. while (!in.atEnd())
    6. {
    7.  
    8. txt=in.readLine(); // read in file
    9. rows=txt.split('\n'); // search for newlines
    10. columns = txt.split('\t'); // search for tabs
    11.  
    12. }
    13.  
    14. int numRows=rows.count();
    15. int numColumns=columns.count()+1;
    16. //int numColumns=rows.first().count('\t')+1;
    17. int row=0;
    18. int column=0;
    19.  
    20. for (int i = 0; i < numRows; ++i) {
    21.  
    22. for (int j = 0; j < numColumns; ++j)
    23. {
    24. // columns=txt[i].split('\t');
    25. // int row = range.topRow() + i;
    26. // int column = range.leftColumn() + j;
    27. if (row < RowCount && column < ColumnCount)
    28. {
    29. in >> row >> column >> txt;
    30. setFormula(row, column, txt);
    31. row++;
    32. column++;
    33. }
    34.  
    35. }
    36. }
    37. }
    To copy to clipboard, switch view to plain text mode 

    It really bugging me since it cannot be that difficult and I want to crack it myself.... When one of us has cracked it why don't we post it here....

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How can i load/display csv file in QTableWidget?(long read, sorry)

    Quote Originally Posted by ChrisW67 View Post
    A CSV file is basic text and the correct Qt tool to read or write it would be QTextStream with some parsing logic to split lines at the delimiters.
    To add to that - there is a regular expression available that can parse csv files (or lines, actually). I found it on the Web some time ago, I'm sure it can be looked up again.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. read QTableWidget cells
    By navid in forum Newbie
    Replies: 8
    Last Post: 4th April 2010, 11:40
  2. How to read rtf file and display it?
    By sunnysun520 in forum Qt Programming
    Replies: 2
    Last Post: 16th May 2009, 15:48
  3. Layout long text in QTableWidget
    By Tamtam in forum Newbie
    Replies: 0
    Last Post: 13th February 2009, 16:09
  4. is qt phonon can read realmedia file and divx file
    By fayssalqt in forum Qt Programming
    Replies: 1
    Last Post: 27th January 2009, 16:42
  5. How to Read and display BMP image using QT
    By agsrinivasan in forum Qt Programming
    Replies: 3
    Last Post: 29th January 2007, 08:14

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.