Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [SOLVED]How to add data into listview from log file
QtWS25 Last Chance

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

Scheduled Pinned Locked Moved General and Desktop
8 Posts 3 Posters 4.4k Views
  • 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 Offline
    D Offline
    Diluka
    wrote on last edited by
    #1

    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
    0
    • A Offline
      A Offline
      andre
      wrote on last edited by
      #2

      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
      0
      • D Offline
        D Offline
        Diluka
        wrote on last edited by
        #3

        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
        0
        • A Offline
          A Offline
          andre
          wrote on last edited by
          #4

          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
          0
          • D Offline
            D Offline
            Diluka
            wrote on last edited by
            #5

            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
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              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
              0
              • D Offline
                D Offline
                Diluka
                wrote on last edited by
                #7

                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
                0
                • SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  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
                  0

                  • Login

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • Users
                  • Groups
                  • Search
                  • Get Qt Extensions
                  • Unsolved