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. How do I get a variable t display in a text edit window?
Forum Updated to NodeBB v4.3 + New Features

How do I get a variable t display in a text edit window?

Scheduled Pinned Locked Moved General and Desktop
17 Posts 3 Posters 19.8k 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.
  • I Offline
    I Offline
    ironmantis7x
    wrote on last edited by
    #8

    [quote author="Lukas Geyer" date="1340054027"]Don't worry, we all started learning Qt somewhen.

    The first stop should always be the "documentation":http://qt-project.org/doc/qt-4.8/qfile.html#details, which usually incorporates a set of examples.

    If they are not sufficient, dig through the forums or the "FAQ":http://qt-project.org/faq - most probably someone else has already asked what you are looking for. If not, feel free to just come back right here and ask.[/quote]

    OK... I am lost here ...
    How do I open a file in Qt for reading and how do I get it to read a line?
    I am reading the docs on QTextStream but I am not getting it.

    Thanks!

    ironmantis7x

    1 Reply Last reply
    0
    • L Offline
      L Offline
      lgeyer
      wrote on last edited by
      #9

      Have you taken a look at the examples, like this one?
      @
      QFile file("in.txt");
      if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
      return;

      QTextStream in(&file);
      while (!in.atEnd()) {
      QString line = in.readLine();
      ...
      }
      @

      1 Reply Last reply
      0
      • I Offline
        I Offline
        ironmantis7x
        wrote on last edited by
        #10

        OK ...

        I am starting (or at least trying) to do things the Qt way!
        It is a lot of fun learning Qt with c++!!

        Now onto my issue...

        I can read the lines in the file and display them, but I broke the word count.
        Can some one help me understand how to count the number of words in a line the Qt way?

        Here i my code:

        @

        #include<iostream>
        #include<cstdio>
        #include<fstream>
        #include<string>
        #include<QtGui>
        #include<QString>
        #include<QTextEdit>
        #include <QTextStream>
        #include <QFile>
        #include "bible3.h"
        #include "ui_bible3.h"

        using namespace std;

        bible3::bible3(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::bible3)
        {
        ui->setupUi(this);
        connect (ui->pushButton, SIGNAL(clicked()), this, SLOT(on_textEdit_textChanged()));

        }

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

        void bible3::on_textEdit_textChanged()
        {

        }

        void bible3::on_pushButton_clicked()
        {
        int numLines = 0;
        int numWords = 0;
        QString text;

        QFile file&#40;"example.csv"&#41;;
        if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
        return;
        
        QTextStream in(&file);
        while (!in.atEnd())
        {
            QString line = in.readLine();
            ++numLines;
            ui ->textEdit->append(line);
            ui -> textEdit ->append(text);
            for(int i=0; i < text.size(); i++)
            {
                    if(text[i] == ' ' || text[i] == '.' || text[i] == ',' ||
                            text[i] == '!' || text[i] == '?' || text[i] == ';')
                    {
                            ++numWords;
        
                            if (numWords > 8)
        
                                    {
                                           ui ->textEdit->append(QString::number(numWords+12));
        
                                    }
        
                            else
                                    {
                                           ui ->textEdit->append(QString::number(numWords+10));
                                    }
        
                    }
        
            }
        
        }
        

        }
        @

        Thanks!!

        ironmantis7x

        1 Reply Last reply
        0
        • I Offline
          I Offline
          ironmantis7x
          wrote on last edited by
          #11

          Well -- I have it working -- sort of ....

          What I need to do is to get the word count to display after it prints each line to the text window. What my program is doing at this moment is displaying all the lines in the file and then printing the count. I know it i a c++ thing at this point and I m a newbie at c++ and Qt. Any help at this point is greatly appreciated. By the way -- I got the lines to print and the word count by using a in.ReadAll() function.

          my code is listed below:

          @

          #include<iostream>
          #include<cstdio>
          #include<fstream>
          #include<string>
          #include<QtGui>
          #include<QString>
          #include<QTextEdit>
          #include <QTextStream>
          #include <QFile>
          #include "bible3.h"
          #include "ui_bible3.h"

          using namespace std;

          bible3::bible3(QWidget *parent) :
          QMainWindow(parent),
          ui(new Ui::bible3)
          {
          ui->setupUi(this);
          connect (ui->pushButton, SIGNAL(clicked()), this, SLOT(on_textEdit_textChanged()));

          }

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

          void bible3::on_textEdit_textChanged()
          {

          }

          void bible3::on_pushButton_clicked()
          {
          int numLines = 0;
          int numWords = 0;
          QString text;

          QFile file&#40;"example.csv"&#41;;
          if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
          return;
          
          QTextStream in(&file);
          while (!in.atEnd())
          {
              QString text = in.readAll();
              //QString line = in.readLine();
              //QString text = in.readLine();
              ++numLines;
              ui ->textEdit->append(text);
              //ui -> textEdit->append("\n");
              for(int i=0; i < text.size(); i++)
              {
                      if(text[i] == ' ' || text[i] == '.' || text[i] == ',' ||
                              text[i] == '!' || text[i] == '?' || text[i] == ';')
                      {
                              ++numWords;
                          cout << numWords << " " << endl;
          
                              if (numWords > 8)
          
                                      {
                                             ui ->textEdit->append(QString::number(numWords+12));
          
                                      }
          
                              else
                                      {
                                             ui ->textEdit->append(QString::number(numWords+10));
                                      }
          
                      }
          
              }
          
          }
          

          }
          @

          thanks!!

          ironmantis7x

          1 Reply Last reply
          0
          • L Offline
            L Offline
            lgeyer
            wrote on last edited by
            #12

            If you want to analyze the file line by line you should stick to readLine(), as readAll() reads the whole file.

            Your word counting code works in general, but be aware that it will treat consecutive seperators as words (eg. <code>word1,,,,word2</code>). It can be simplified using QString::split() with a regular expression, and consecutive seperators can be filtered out by removing empty strings from the list.
            @
            QTextStream in(&file);
            while (!in.atEnd())
            {
            // read line from file
            QString line = in.readLine();

            // split the line into single words at .,!?; using a regular expression into a list
            QStringList words = line.split(QRegExp("[ \\.,!\\?;]"));
            
            // remove empty entries from list, which have be produced by consecutive seperators
            words.remove("");
            
            // count the number of remaining entries in the list
            int numWords = words.count();
            ...
            

            }
            @
            If you are aiming for a robust all-purpose CSV file parser be aware that there is much more than this (quotes, nested seperators, ...) and you should consider using one of the already existing CSV parsers.

            1 Reply Last reply
            0
            • I Offline
              I Offline
              ironmantis7x
              wrote on last edited by
              #13

              [quote author="Lukas Geyer" date="1340603406"]If you want to analyze the file line by line you should stick to readLine(), as readAll() reads the whole file.

              Your word counting code works in general, but be aware that it will treat consecutive seperators as words (eg. <code>word1,,,,word2</code>). It can be simplified using QString::split() with a regular expression, and consecutive seperators can be filtered out by removing empty strings from the list.
              @
              QTextStream in(&file);
              while (!in.atEnd())
              {
              // read line from file
              QString line = in.readLine();

              // split the line into single words at .,!?; using a regular expression into a list
              QStringList words = line.split(QRegExp("[ \\.,!\\?;]"));
              
              // remove empty entries from list, which have be produced by consecutive seperators
              words.remove("");
              
              // count the number of remaining entries in the list
              int numWords = words.count();
              ...
              

              }
              @
              If you are aiming for a robust all-purpose CSV file parser be aware that there is much more than this (quotes, nested seperators, ...) and you should consider using one of the already existing CSV parsers.[/quote]

              OK ... that helped a lot!!! Thank you so much. My code is taking shape in a Qt fashion. The only thing is that the word count puts in a carriage return after each count. How do I get it to all stay on one line?

              ironmantis7x

              1 Reply Last reply
              0
              • L Offline
                L Offline
                lgeyer
                wrote on last edited by
                #14

                append() adds a new paragraph to the end of the text edit. So you either pass the entire text you want to be added (<code>ui->textEdit->append(line + QString::number(numWords))</code>) or you use insertPlainText(), which does what one might expect from append(), add text at the current cursor position.

                1 Reply Last reply
                0
                • I Offline
                  I Offline
                  ironmantis7x
                  wrote on last edited by
                  #15

                  [quote author="Lukas Geyer" date="1340653139"]append() adds a new paragraph to the end of the text edit. So you either pass the entire text you want to be added (<code>ui->textEdit->append(line + QString::number(numWords))</code>) or you use insertPlainText(), which does what one might expect from append(), add text at the current cursor position.[/quote]

                  insertPlainText works perfect for my application and it is coming out the way that I need it to!!

                  Thanks so much for your help and guidance!!

                  ironmantis7x

                  1 Reply Last reply
                  0
                  • L Offline
                    L Offline
                    lgeyer
                    wrote on last edited by
                    #16

                    You're welcome!

                    1 Reply Last reply
                    0
                    • I Offline
                      I Offline
                      ironmantis7x
                      wrote on last edited by
                      #17

                      Code is working well now ...
                      I am branching out to put in a menu bar and make a combo box for my program that can pick which *.csv file to read! So far so good!

                      I could not have done this effort with out you guys!

                      Thanks!

                      ironmantis7x

                      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