PDA

View Full Version : Reading/writing QTableWidget



Salazaar
18th June 2007, 20:28
Hi. I've got a question. If there's no pointers to read/write to file a QTableWidget, how can I do it? Regards

wysota
18th June 2007, 21:22
What do you mean by "write a QTableWidget to a file"?

Salazaar
18th June 2007, 21:31
I have a QTableWidget in my program, and I want to save to file/read from file what it contains. Clear, now?;)

fedcer
18th June 2007, 23:22
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:


QTextStream out(&file);
for (int i = 0; i < rowCount(); ++i) {
if (i > 0)
out << '\n';
for (int j = 0; j < columnCount(); ++j) {
if (j > 0)
out << '\t';
QTableWidgetItem *cell = item(i, j);
out << cell->text();
}
}

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:


QTextStream in(&file);
QString str = in.readAll();
QStringList rows = str.split('\n');

int numRows = rows.count();
int numColumns = rows.first().count('\t') + 1;
setRowCount(numRows);
setColumnCount(numColumns);

for (int i = 0; i < numRows; ++i) {
QStringList columns = rows[i].split('\t');
for (int j = 0; j < numColumns; ++j) {
QTableWidgetItem *cell = item(i, j);
cell->setText(columns[j]);
}
}

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.

jacek
18th June 2007, 23:54
Please, don't give fish to Salazaar.

wysota
19th June 2007, 01:04
Please, don't give fish to Salazaar.

Tell him "search the forum before asking questions" instead ;)

Salazaar
19th June 2007, 11:29
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

wysota
19th June 2007, 12:07
You shouldn't be looking at the docs. You should be reading it ;)

Salazaar
19th June 2007, 12:22
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

wysota
19th June 2007, 13:20
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 :cool:

fedcer
19th June 2007, 21:56
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:


QTableWidget table1;
QTextStream out(&file);
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';
QTableWidgetItem *cell = table1.item(i, j);
out << cell->text();
}
}

Salazaar
19th June 2007, 22:11
That's what I like. Full, clear explanation of a problem:cool: Thanks,
but there are errors that rowCount and columnCount are undeclared, do I have to declare them before?
Regards

pdolbey
19th June 2007, 23:05
Questions - no fish.

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

Pete

ad5xj
22nd June 2007, 00:16
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.

jacek
22nd June 2007, 02:04
Isn't THAT the point.
Not exactly, sometimes it's better to guide a newbie by asking questions.

wysota
23rd June 2007, 14:20
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?