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. Getting a specific text from a text file and inserting to a QStringList!
Forum Updated to NodeBB v4.3 + New Features

Getting a specific text from a text file and inserting to a QStringList!

Scheduled Pinned Locked Moved Solved General and Desktop
qt creatortextfilereadvalue
10 Posts 3 Posters 4.5k Views 1 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.
  • L Offline
    L Offline
    Lasith
    wrote on 14 Dec 2017, 11:52 last edited by Lasith
    #1

    IN my qt c++ application I have created a functionality to upload and read text files! I want to get a specific part of selected text lines to a QStringLIst!
    Following is my code

    MainWindow.h

    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H
    
    #include <QMainWindow>
    
    namespace Ui {
    class MainWindow;
    }
    
    class MainWindow : public QMainWindow
    {
        Q_OBJECT
    
    public:
        QString filepath;
        void readValues();
        QStringList values;
        explicit MainWindow(QWidget *parent = 0);
        ~MainWindow();
    
    private slots:
        void on_pushButton_clicked();
    
        void on_pushButton_2_clicked();
    
    private:
        Ui::MainWindow *ui;
    };
    

    MainWindow.cpp

    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    #include "QFileDialog"
    #include "QTextStream"
    
    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
    }
    
    MainWindow::~MainWindow()
    {
        delete ui;
    }
    
    void MainWindow::on_pushButton_clicked()
    {
        filepath=QFileDialog::getOpenFileName(this, "Get Any File");
            ui->lineEdit->setText(filepath);
    }
    
    void MainWindow::readValues(){
        QFile file(filepath);
           if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
               return;
    
        QTextStream in(&file);
              while (!in.atEnd()) {
    
                  QString line = in.readLine();
               
                  values<<line;
                  }
           }
              ui->label->setText(QString::number(values.size()));
    }
    
    void MainWindow::on_pushButton_2_clicked()
    {
        readValues();
    }
    

    The text file(names.txt) I read have the following format!

    <h>Hello<h>
    <name>John</name>
    <h>Hello<h>
    <name>Smith</name>
    <h>Hello<h>
    <name>Mary</name>
    <h>Hello<h>
    <name>Anne</name>
    <h>Hello<h>
    <name>Jennifer</name>
    <h>Hello<h>

    Currenly I get all the text lines into the QStringList!
    I want to get the words(without <name></name> tags) John,Smith,Marry,Anne,Jennifer into the QStringList named values! How can I do it?

    1 Reply Last reply
    0
    • V Offline
      V Offline
      VRonin
      wrote on 14 Dec 2017, 12:03 last edited by VRonin
      #2
      void MainWindow::readValues(){
      QFile file(filepath);
      if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
      return;
      const QRegularExpression nameExp(R"***(<name>(.+)<\/name>)***");
      QTextStream in(&file);
      QString line;
      while (stream.readLineInto(&line)) {
      const auto nameMatch = nameExp.match(line);
      if(nameMatch.hasMatch())
      values << nameMatch.captured(1);
      }
       ui->label->setText(QString::number(values.size()));
      }
      

      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
      ~Napoleon Bonaparte

      On a crusade to banish setIndexWidget() from the holy land of Qt

      L 1 Reply Last reply 14 Dec 2017, 12:11
      4
      • V VRonin
        14 Dec 2017, 12:03
        void MainWindow::readValues(){
        QFile file(filepath);
        if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
        return;
        const QRegularExpression nameExp(R"***(<name>(.+)<\/name>)***");
        QTextStream in(&file);
        QString line;
        while (stream.readLineInto(&line)) {
        const auto nameMatch = nameExp.match(line);
        if(nameMatch.hasMatch())
        values << nameMatch.captured(1);
        }
         ui->label->setText(QString::number(values.size()));
        }
        
        L Offline
        L Offline
        Lasith
        wrote on 14 Dec 2017, 12:11 last edited by
        #3

        @VRonin Thanx I get an errors stating
        "R was not declared in this scope"
        "stream was not declared in this scope"
        "nameMatch does not name a type"
        "nameMatch was not declared in this scope"

        V 1 Reply Last reply 14 Dec 2017, 12:14
        0
        • L Lasith
          14 Dec 2017, 12:11

          @VRonin Thanx I get an errors stating
          "R was not declared in this scope"
          "stream was not declared in this scope"
          "nameMatch does not name a type"
          "nameMatch was not declared in this scope"

          V Offline
          V Offline
          VRonin
          wrote on 14 Dec 2017, 12:14 last edited by VRonin
          #4

          "R was not declared in this scope"

          Old compiler? that's C++11. use const QRegularExpression nameExp("<name>(.+)<\\/name>"); then

          "stream was not declared in this scope"

          change while (stream.readLineInto(&line)) into while (in.readLineInto(&line))

          "nameMatch does not name a type"

          again old compiler const QRegularExpressionMatch nameMatch = nameExp.match(line);

          "nameMatch was not declared in this scope"

          The above solves this too

          "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
          ~Napoleon Bonaparte

          On a crusade to banish setIndexWidget() from the holy land of Qt

          L 1 Reply Last reply 14 Dec 2017, 12:20
          2
          • V VRonin
            14 Dec 2017, 12:14

            "R was not declared in this scope"

            Old compiler? that's C++11. use const QRegularExpression nameExp("<name>(.+)<\\/name>"); then

            "stream was not declared in this scope"

            change while (stream.readLineInto(&line)) into while (in.readLineInto(&line))

            "nameMatch does not name a type"

            again old compiler const QRegularExpressionMatch nameMatch = nameExp.match(line);

            "nameMatch was not declared in this scope"

            The above solves this too

            L Offline
            L Offline
            Lasith
            wrote on 14 Dec 2017, 12:20 last edited by
            #5

            @VRonin Thanx if there are several blank spaces before "<name>" how do I need to change the code?

            V 1 Reply Last reply 14 Dec 2017, 12:21
            0
            • L Lasith
              14 Dec 2017, 12:20

              @VRonin Thanx if there are several blank spaces before "<name>" how do I need to change the code?

              V Offline
              V Offline
              VRonin
              wrote on 14 Dec 2017, 12:21 last edited by
              #6

              add \\s* wherever there could be empty space

              "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
              ~Napoleon Bonaparte

              On a crusade to banish setIndexWidget() from the holy land of Qt

              L 1 Reply Last reply 14 Dec 2017, 12:24
              2
              • V VRonin
                14 Dec 2017, 12:21

                add \\s* wherever there could be empty space

                L Offline
                L Offline
                Lasith
                wrote on 14 Dec 2017, 12:24 last edited by
                #7

                @VRonin unfortunately there is another error stating "class QTextStream has no memebr named readLineInto"

                1 Reply Last reply
                0
                • V Offline
                  V Offline
                  VRonin
                  wrote on 14 Dec 2017, 12:33 last edited by
                  #8

                  What version of Qt are you using?

                  Replace it with your

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

                  "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                  ~Napoleon Bonaparte

                  On a crusade to banish setIndexWidget() from the holy land of Qt

                  L 1 Reply Last reply 14 Dec 2017, 12:35
                  3
                  • V VRonin
                    14 Dec 2017, 12:33

                    What version of Qt are you using?

                    Replace it with your

                     while (!in.atEnd()) {
                    line = in.readLine();
                    
                    L Offline
                    L Offline
                    Lasith
                    wrote on 14 Dec 2017, 12:35 last edited by
                    #9

                    @VRonin Thanx alot! I use Qt 5.2

                    JKSHJ 1 Reply Last reply 14 Dec 2017, 22:51
                    0
                    • L Lasith
                      14 Dec 2017, 12:35

                      @VRonin Thanx alot! I use Qt 5.2

                      JKSHJ Online
                      JKSHJ Online
                      JKSH
                      Moderators
                      wrote on 14 Dec 2017, 22:51 last edited by JKSH
                      #10

                      @Lasith said in Getting a specific text from a text file and inserting to a QStringList!:

                      I use Qt 5.2

                      That's not supported anymore.

                      Is it feasible for you to upgrade to a newer version? (Qt 5.10.0 was released last week)

                      Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

                      1 Reply Last reply
                      2

                      1/10

                      14 Dec 2017, 11:52

                      • Login

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