Hi. I've got a question. If there's no pointers to read/write to file a QTableWidget, how can I do it? Regards
Hi. I've got a question. If there's no pointers to read/write to file a QTableWidget, how can I do it? Regards
What do you mean by "write a QTableWidget to a file"?
I have a QTableWidget in my program, and I want to save to file/read from file what it contains. Clear, now?
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:
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.Qt Code:
for (int i = 0; i < rowCount(); ++i) { if (i > 0) out << '\n'; for (int j = 0; j < columnCount(); ++j) { if (j > 0) out << '\t'; out << cell->text(); } }To copy to clipboard, switch view to plain text mode
To read from that same stream the code would be:
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.Qt Code:
int numRows = rows.count(); int numColumns = rows.first().count('\t') + 1; setRowCount(numRows); setColumnCount(numColumns); for (int i = 0; i < numRows; ++i) { for (int j = 0; j < numColumns; ++j) { cell->setText(columns[j]); } }To copy to clipboard, switch view to plain text mode
Last edited by fedcer; 18th June 2007 at 22:47.
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.
You shouldn't be looking at the docs. You should be reading it
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
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:
QTableWidget table1; for (int i = 0; i < table1.rowCount(); ++i) { if (i > 0) out << '\n'; for (int j = 0; j < table1.columnCount(); ++j) { if (j > 0) out << '\t'; out << cell->text(); } }To copy to clipboard, switch view to plain text mode
Last edited by fedcer; 19th June 2007 at 21:06.
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
Questions - no fish.
What class is table1?
What methods does that class have?
Why can't you see them?
Pete
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.
We're not flaming anyone.
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?Salazar is entitled to ask questions like this.
Bookmarks