Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

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

    General and Desktop
    3
    8
    4026
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • D
      Diluka last edited by

      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

      1 Reply Last reply Reply Quote 0
      • A
        andre last edited by

        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.

        1 Reply Last reply Reply Quote 0
        • D
          Diluka last edited by

          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

          1 Reply Last reply Reply Quote 0
          • A
            andre last edited by

            What changes did you make to your code?

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

            1 Reply Last reply Reply Quote 0
            • D
              Diluka last edited by

              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 )!

              1 Reply Last reply Reply Quote 0
              • SGaist
                SGaist Lifetime Qt Champion last edited by

                Hi,

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

                Interested in AI ? www.idiap.ch
                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                1 Reply Last reply Reply Quote 0
                • D
                  Diluka last edited by

                  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);
                  

                  @

                  1 Reply Last reply Reply Quote 0
                  • SGaist
                    SGaist Lifetime Qt Champion last edited by

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

                    Interested in AI ? www.idiap.ch
                    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                    1 Reply Last reply Reply Quote 0
                    • First post
                      Last post