tags:

views:

311

answers:

3

Hello world,

I have a table view with three columns; I have just passed to write into text file using this code

QFile file("/home/hamad/lesson11.txt");
if(!file.open(QIODevice::WriteOnly)) {
    QMessageBox::information(0,"error",file.errorString());
}
QString dd;

for(int row=0; row < model->rowCount(); row++) {
     dd = model->item(row,0)->text() +  ","
                 + model->item(row,1)->text() +  ","
                 + model->item(row,2)->text();

     QTextStream out(&file);
     out << dd << endl;
 }

But I'm not succeed to read the same file again, I tried this code but I don't know where is the problem in it

QFile file("/home/hamad/lesson11.txt");
QTextStream in(&file);
QString line = in.readLine();
while(!in.atEnd()) {

    QStringList  fields = line.split(",");

    model->appendRow(fields);

}

Any help please ?

+1  A: 

Do you open the file again like you did the first time? I think that might be your issue.

SB
I couldn't open the file from Qt; but I'm sure the writing function is working perfectly by open the txt file using geedit. Any help ??
mosg has a good point above, but my question was why aren't you calling file.open again before trying to read your file? You do it before writing it, so why wouldn't you have to do it before reading it? Your code in the post doesn't do that.
SB
I don't want to open the text file, I want to read the data what are in the text file then bind them into tableview
You have to open the file to start reading it. Look at your code closely. When you write data, you first call file.open(QIODevice::WriteOnly) and then write data to it. Similarly, to get data out of a file, you will need to call file.open(QIODevice::ReadOnly) and then read the data. You can do whatever you want with the data after that. These are standard operations when doing File IO in most languages.
SB
Also you may read this docs for help: http://qt.nokia.com/doc/4.6/qfile.html#details
mosg
SB and mosg I highly regarding your efforts; the open file statement has been added; my problem is I can't bind the text data to the model because model.appendRow() function dosn't support QString List
+2  A: 

You have to replace string line

QString line = in.readLine();

into while:

QFile file("/home/hamad/lesson11.txt");
if(!file.open(QIODevice::ReadOnly)) {
    QMessageBox::information(0, "error", file.errorString());
}

QTextStream in(&file);

while(!in.atEnd()) {
    QString line = in.readLine();    
    QStringList fields = line.split(",");    
    model->appendRow(fields);    
}

file.close();
mosg
his bug seemed to also be that he wasn't calling file.open again like you do in your code.
SB
mosg
A: 

I solved by myself, I will post the currect code as a refernce

 while(!in.atEnd()){
         QString line = in.readLine();
         QList<QStandardItem *> items;
         QStringList fields = line.split(",");
         foreach (QString text, fields)
             items.append(new QStandardItem((QString)text));

         model->appendRow(items);
     }
you solved it after a lot of help from mosg and myself. don't pretend you did it on your own.
SB
Many Many Many Thanks mosg and SB to make that done; I really appreciate your hints, help and support. I wanna say something from my heart(thank you both very much and I'm happy to meet you and I hope to see you again ^_^