Results 1 to 16 of 16

Thread: Reading/writing QTableWidget

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    May 2007
    Posts
    315
    Thanks
    1
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Reading/writing QTableWidget

    Hi. I've got a question. If there's no pointers to read/write to file a QTableWidget, how can I do it? Regards

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

    Default Re: Reading/writing QTableWidget

    What do you mean by "write a QTableWidget to a file"?

  3. #3
    Join Date
    May 2007
    Posts
    315
    Thanks
    1
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Reading/writing QTableWidget

    I have a QTableWidget in my program, and I want to save to file/read from file what it contains. Clear, now?

  4. #4
    Join Date
    Jun 2007
    Posts
    8
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Reading/writing QTableWidget

    If you look at the QTableWidget documentation (http://doc.trolltech.com/4.3/qtablewidget.html) you'll see that there is a QTableWidgetItem item(int row, int column) function. So, you could easily iterate through all the cells with loops such as these:
    Qt Code:
    1. QTextStream out(&file);
    2. for (int i = 0; i < rowCount(); ++i) {
    3. if (i > 0)
    4. out << '\n';
    5. for (int j = 0; j < columnCount(); ++j) {
    6. if (j > 0)
    7. out << '\t';
    8. QTableWidgetItem *cell = item(i, j);
    9. out << cell->text();
    10. }
    11. }
    To copy to clipboard, switch view to plain text mode 
    this is just an example in which the program writes to a stream the contents of the table, separating each row with a newline and each column of each row with a tab.
    To read from that same stream the code would be:
    Qt Code:
    1. QTextStream in(&file);
    2. QString str = in.readAll();
    3. QStringList rows = str.split('\n');
    4.  
    5. int numRows = rows.count();
    6. int numColumns = rows.first().count('\t') + 1;
    7. setRowCount(numRows);
    8. setColumnCount(numColumns);
    9.  
    10. for (int i = 0; i < numRows; ++i) {
    11. QStringList columns = rows[i].split('\t');
    12. for (int j = 0; j < numColumns; ++j) {
    13. QTableWidgetItem *cell = item(i, j);
    14. cell->setText(columns[j]);
    15. }
    16. }
    To copy to clipboard, switch view to plain text mode 
    This is just an example taken from old code I had. You should adapt it to the format in which you prefer to store the data, which could even be in binary.
    Last edited by fedcer; 18th June 2007 at 22:47.

  5. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Reading/writing QTableWidget

    Please, don't give fish to Salazaar.

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

    Default Re: Reading/writing QTableWidget

    Quote Originally Posted by jacek View Post
    Please, don't give fish to Salazaar.
    Tell him "search the forum before asking questions" instead

  7. #7
    Join Date
    May 2007
    Posts
    315
    Thanks
    1
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Reading/writing QTableWidget

    Quote Originally Posted by jacek View Post
    Please, don't give fish to Salazaar.
    Don't give fishes? Till I cried, though When I saw the exact code, and didn't see the other nick, I was very surprised that you're giving me code, not saying "Look at the docs" - not I'm a bit smarter than before, an firstable I'm looking at docs - when there's no answer at this question or I don't understand, I'm asking here Regards
    Last edited by Salazaar; 19th June 2007 at 10:45.

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

    Default Re: Reading/writing QTableWidget

    You shouldn't be looking at the docs. You should be reading it

  9. #9
    Join Date
    May 2007
    Posts
    315
    Thanks
    1
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Reading/writing QTableWidget

    Sure But in this code that fedcer posted, there's no definition which of few QTableWidgets (if I have few) will be wrote or read. There's only QTableWidgetItem but what if I have few QTableWidgets? Regards

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

    Default Re: Reading/writing QTableWidget

    Quote Originally Posted by Salazaar View Post
    But in this code that fedcer posted, there's no definition which of few QTableWidgets (if I have few) will be wrote or read.
    Sure there is. I don't have trouble determining which widget will be processed - it's the one pointed by "this"

    @fedcer: That's why giving the code was a bad idea... Now you have to explain it

  11. #11
    Join Date
    Jun 2007
    Posts
    8
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Reading/writing QTableWidget

    As wysota said it's the QTableWidget poited by "this". The example code I posted was designed to work as a member of a Subclass of QTableWidget. If you want to work from outside you have to specify the QTableWidget you are referring to by calling QTableWidget's specific functions (rowCount(), columnCount(), item(), setRowCount(), setColumnCount(), ...) as member of that table. That is, in fact, the only way it could work if you decide to use that function from outside.
    For example, the writing code rewritten would be:
    Qt Code:
    1. QTableWidget table1;
    2. QTextStream out(&file);
    3. for (int i = 0; i < table1.rowCount(); ++i) {
    4. if (i > 0)
    5. out << '\n';
    6. for (int j = 0; j < table1.columnCount(); ++j) {
    7. if (j > 0)
    8. out << '\t';
    9. QTableWidgetItem *cell = table1.item(i, j);
    10. out << cell->text();
    11. }
    12. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by fedcer; 19th June 2007 at 21:06.

  12. #12
    Join Date
    May 2007
    Posts
    315
    Thanks
    1
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Reading/writing QTableWidget

    That's what I like. Full, clear explanation of a problem Thanks,
    but there are errors that rowCount and columnCount are undeclared, do I have to declare them before?
    Regards

Similar Threads

  1. QTableWidget (resizing rows, turning off selection, etc.)
    By kiss-o-matic in forum Qt Programming
    Replies: 6
    Last Post: 11th January 2007, 01:57
  2. QTableWidget issues
    By Djony in forum Qt Programming
    Replies: 42
    Last Post: 19th December 2006, 23:27
  3. print QTableWidget
    By chak_med in forum Qt Programming
    Replies: 3
    Last Post: 4th November 2006, 18:46
  4. QTableWidget editing question
    By Trasmeister in forum Qt Programming
    Replies: 1
    Last Post: 20th September 2006, 18:46
  5. Replies: 6
    Last Post: 5th March 2006, 21:05

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
  •  
Qt is a trademark of The Qt Company.