Results 1 to 16 of 16

Thread: Reading/writing QTableWidget

  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,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: 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,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: 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,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: 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,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: 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

  13. #13
    Join Date
    Nov 2006
    Location
    Shrewsbury, UK
    Posts
    97
    Thanks
    3
    Thanked 11 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Reading/writing QTableWidget

    Questions - no fish.

    What class is table1?
    What methods does that class have?
    Why can't you see them?

    Pete

  14. #14
    Join Date
    Jun 2007
    Location
    Louisiana
    Posts
    77
    Thanks
    1
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Reading/writing QTableWidget

    There is a reason why they call this the "newbie" forum. Save your flaming and impatience for the expert level guys in another forum. Salazar is entitled to ask questions like this. Even if he should have and could have looked at the docs or read them, or even if he could have or should have searched the other postings, the point of this forum is to bring "newbies" along to be mature and experienced with Qt - that is unless the forum is just a front for bullies who can't tolerate someone just getting started and are not as "smart" as they are.

    Even if he did not learn something from the example, I did. And, I think someone else may as well. Isn't THAT the point.

  15. #15
    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

    Quote Originally Posted by ad5xj View Post
    Isn't THAT the point.
    Not exactly, sometimes it's better to guide a newbie by asking questions.

  16. #16
    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: Reading/writing QTableWidget

    Quote Originally Posted by ad5xj View Post
    There is a reason why they call this the "newbie" forum. Save your flaming and impatience for the expert level guys in another forum.
    We're not flaming anyone.
    Salazar is entitled to ask questions like this.
    Sure he is. But he should then follow our guides and not blindly rewrite the code given and come back saying it doesn't work. If you follow our efforts in all the threads, you'll notice that since the very beginning we've been pointing out that the problem is the author doesn't understand C++ and he should learn it first before using Qt. Most of his questions fit more to the "general programming" forum and not to the "newbie" one. Nevertheless we keep answering all his questions trying to teach him a bit of C++ and Qt. Nobody is throwing flames here. I can understand the frustration of Salazaar or you but I can also understand the frustration of people trying to help him and failing because of his stubbornness not to read anything and have the answer given to him on a golden plate. He has proper books, he can read them. But he prefers to ask us - this is fine, but we'll be pointing him back to the book because we won't give him a full C++ class here - that's not the point of this forum. That's the goal of writing and buying books, isn't it?

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.