Qt Forum

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

    Unsolved Code to return a particular number of line along with the text from a document in qt

    The Lounge
    3
    8
    1198
    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.
    • J
      Joel George last edited by

      We are trying to code a program that will check for certain rules and find out violations of those rules from a text file. What we need help is with how to find in what line number the rule has been violated. How to return the line number of that particular line in which the rule has been violated, along with that text ?

      raven-worx 1 Reply Last reply Reply Quote 0
      • raven-worx
        raven-worx Moderators @Joel George last edited by raven-worx

        @Joel-George
        i don't know how anybody here should help you, since this heavily depends on your implementation of your rule-violation-checking?!
        You should be more precise with your question regarding Qt mechanics/classes i guess.

        Show the code where you read in the lines for example.

        --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
        If you have a question please use the forum so others can benefit from the solution in the future

        1 Reply Last reply Reply Quote 0
        • J
          Joel George last edited by

          @raven-worx Sure.. this is the coding... Can you please tell how to print in the GUI the line number along with the content from that, the rule is violated.

          #include "mainwindow.h"
          #include "ui_mainwindow.h"
          #include <QTextStream>
          #include <QMessageBox>
          #include <QFileDialog>
          #include <QDebug>

          MainWindow::MainWindow(QWidget* parent) :
          QMainWindow(parent),
          ui(new Ui::MainWindow) {
          ui->setupUi(this);
          }

          MainWindow::~MainWindow() {
          delete ui;
          }

          #include <QFile>
          void CompareRules(QString OnePara ) {
          //QString a="//&&hai";
          // check the rules
          if (OnePara.contains("//"))
          QMessageBox::warning(NULL, ("My Application"), ("ERROR.\n""Rule Violated!" ));

          }

          void MainWindow::on_pushButton_clicked()
          {
          QString fileName = QFileDialog::getOpenFileName(this, tr("Opentext File"),
          QDir::currentPath(),
          tr("text Files (*.txt)"));
          if (fileName.isEmpty()) {
          return;
          }

          QFile inputFile(fileName);
          QString Para;
          if (inputFile.open(QIODevice::ReadOnly)) {
            QTextStream in(&inputFile);
            while (!in.atEnd()) {\
              QString line = in.readLine();
              Para += line;
              qDebug()<<line;
              qDebug()<<Para;
              if (line!=NULL) { // we found blank line, so test and clear
                      qDebug()<<line;
                CompareRules(Para); //check
                qDebug() << "ParaRead: " << Para;
                //Para = ""; //clear
              }
            }
            inputFile.close();
          }
          

          }

          raven-worx 1 Reply Last reply Reply Quote 0
          • raven-worx
            raven-worx Moderators @Joel George last edited by

            @Joel-George
            simply use a counting variable:

            int lineCount = 1;
            while (!in.atEnd()) {
            // read line
            qDebug() << lineCount << line;
            ++lineCount; // increase count at the very end of the loop
            }
            

            --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
            If you have a question please use the forum so others can benefit from the solution in the future

            1 Reply Last reply Reply Quote 1
            • J
              Joel George last edited by

              @raven-worx The current text file has content saying
              Let the Input File be s this:
              Hi
              //
              bye....

              Expected Output:

              Line 2: ERROR Rule Violated!

              raven-worx 1 Reply Last reply Reply Quote 0
              • raven-worx
                raven-worx Moderators @Joel George last edited by

                @Joel-George
                is this a question? :)
                Or a statement that it is working?

                --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
                If you have a question please use the forum so others can benefit from the solution in the future

                J 1 Reply Last reply Reply Quote 0
                • J
                  Joel George @raven-worx last edited by

                  The above post was an example of how the output is expected and it was a question :D In the console this line count logic works. Can you help us with printing the same result in GUI ?

                  1 Reply Last reply Reply Quote 0
                  • Yves Maurischat
                    Yves Maurischat last edited by Yves Maurischat

                    First, change your rule checking method to return a boolean value instead of displaying the message box directly:

                    bool CompareRules(QString OnePara ) { 
                        if (OnePara.contains("//"))
                              return false;
                    
                        return true;
                    }
                    

                    Then add a counter variable and check the result of the rule checking method:

                    if (inputFile.open(QIODevice::ReadOnly)) {
                      QTextStream in(&inputFile);
                      int counter = 0;
                      while (!in.atEnd()) {
                        counter++;
                        QString line = in.readLine();
                        Para += line;
                        if (line!=NULL) {
                            if (!CompareRules(Para))  {
                                QMessageBox::warning(NULL, "My Application", QString("ERROR.\nRule Violated in line %1!").arg(counter) );
                                return;
                            }
                        }
                    
                    1 Reply Last reply Reply Quote 0
                    • First post
                      Last post