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



  • 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 ?


  • Moderators

    @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.



  • @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();
    }
    

    }


  • Moderators

    @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
    }
    


  • @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!


  • Moderators

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



  • 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 ?



  • 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;
            }
        }
    

Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.