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. QFile read only lines containing specific search term
Qt 6.11 is out! See what's new in the release blog

QFile read only lines containing specific search term

Scheduled Pinned Locked Moved Solved General and Desktop
8 Posts 4 Posters 634 Views 2 Watching
  • 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.
  • R Offline
    R Offline
    RainyDays
    wrote on last edited by
    #1

    Hello!

    When i open a file to read all i want to select specific rows "to be saved" as an output to QStringList.
    So when i read through a file and a line has ex. "orange" i want to save that line to a list.

    My read file is a CSV and i have my search terms in a combo box.

    Christian EhrlicherC 1 Reply Last reply
    0
    • R RainyDays

      Hello!

      When i open a file to read all i want to select specific rows "to be saved" as an output to QStringList.
      So when i read through a file and a line has ex. "orange" i want to save that line to a list.

      My read file is a CSV and i have my search terms in a combo box.

      Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @RainyDays said in QFile read only lines containing specific search term:

      So when i read through a file and a line has ex. "orange" i want to save that line to a list.

      And what's the exact problem you face? Read the line, check for your condition and add the values to your stringlist.

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      1 Reply Last reply
      1
      • R Offline
        R Offline
        RainyDays
        wrote on last edited by
        #3

        Is it easier to do this while reading or when the data is in the list?

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Hi,

          While reading, no need to load useless data.

          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
          1
          • R Offline
            R Offline
            RainyDays
            wrote on last edited by
            #5

            Ok, so the output from the file needs to be limited by the condition.

            1 Reply Last reply
            0
            • R Offline
              R Offline
              RainyDays
              wrote on last edited by
              #6

              Ok, so here is what i came up with :)

                if (database.open(QFile::ReadOnly | QIODevice::Text))
                         {
                             while(!fileOutputStream.atEnd())
                             {
                             lineData = fileOutputStream.readLine();
                              if (lineData.contains(condition))
                              {
                              fileOutput.append(lineData.split("\n"));
                              }
              
                             }
                             database.close();
                         }
              
              
              1 Reply Last reply
              0
              • C Offline
                C Offline
                ChrisW67
                wrote on last edited by
                #7

                I assume the thing named fileOutput is your desired QStringList.
                What is the relationship between your input data, presumably database, and the text stream, fileOutputStream presumably a QTextStream, you are attempting to read from.

                BTW, the string returned by QTextStream::readLine() does not contain any end-of-line markers, so there is not need to try to remove them.

                1 Reply Last reply
                1
                • R Offline
                  R Offline
                  RainyDays
                  wrote on last edited by
                  #8

                  Hello!

                  Yes you are correct.
                  the setup is like this

                      QFile database("/RDS.csv");
                      QTextStream fileOutputStream(&database);
                      QString lineData;
                      QStringList fileOutput;
                  

                  And my search condition is

                  QString condition = ui->comboBox->currentText();
                  

                  Also i don't need to hunt the end of line markers.
                  I had this setup when i was reading all data before.
                  I wanted to take the first column if the row contained a search term.
                  But for this it works fine with

                  fileOutput.append(lineData);
                  
                  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