[SOLVED]How to add data into listview from log file



  • Hi
    I am very beginer to the Qt development and i faced problem

    I have a log file and it had some data in that file then i want to display in that data in my listView i try but still i havent proper way to do that this is my code
    @
    model = new QStringListModel();

    //Read data from file in the system
    

    QFile file("File path");
    QTextStream in(&file);
    while(!in.atEnd()){
    QString line = in.readLine();
    QList<QStandardItem *> items;
    QStringList fields = line.split(">");
    model->setStringList(fields);

    }

      ui.listView->setModel(model);
    

    @

    there are no errors but no data display in a listview please anyone have better idea about that please kindly help me to solve that problem

    Thanks
    Diluka

    Edit: please put @ tags around code sections; Andre



  • You seem to be confused about the type of model you want to use. Do you want to have one item per row, or do you need columns? If you need columns (and it looks like you do), then QStringListModel isn't suitable for your purpose.



  • Thanks for the fast reply andre But still data not display in my listview please can you tell me what steps i want to change and i want to add one item per row i found that method my self i think some steps want to be change but i don't know how to change it

    thanks again



  • What changes did you make to your code?

    I am not going to give you ready made code, even though I can.



  • I found the error my self now data display in listview but my expected out put is not in there after one item display in listview there are huge gap between first and second items in listview any body have better idea about that.

    thanks

    my current code

    @ model2 = new QStandardItemModel();

    QFile file(" file name");
    file.open(QIODevice::ReadOnly | QIODevice::Text);

    QTextStream in(&file);
    while(!in.atEnd()){
    QString line = in.readLine();

    QList<QStandardItem *> items;
    QStringList fields = line.split(">");

    QStringList fields3 = fields.filter("#");

    foreach (QString text, fields3)
    items.append(new QStandardItem((QString)text));

    model2->appendRow(items);

    }
    ui.listView->setModel(model2); @
    !http://tinypic.com/view.php?pic=102tru9&s=6(Error Image )!


  • Lifetime Qt Champion

    Hi,

    You don't check for empty strings when appending items, you might be seeing a result from that.



  • Thank you all problem was solved i got my expected output

    correct code

    @

    model2 = new QStandardItemModel();

    QFile file("file name ");
    file.open(QIODevice::ReadOnly | QIODevice::Text);

    QTextStream in(&file);

    while(!in.atEnd()){
    QString line = in.readLine();

    QList<QStandardItem *> items;
    QStringList fields = line.split(">");

                 QStringList fields3 = fields.filter("#") ;    
      
    
    
       foreach (QString text, fields3)
       {
          items.append(new QStandardItem((QString)text));
       }
    
    
          if(items.length()>0)
                        {
             model2->appendRow(items);
          }
    

    }

        ui.listView->setModel(model2);
    

    @


  • Lifetime Qt Champion

    Great !
    Don't forget to update the thread's title to solved so other forum users may know that a solution has been found


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.