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



  • Hello all!

    I am trying to get a variable (string variable) to display in a text edit window in Qt. here is my code:

    @

    #include<iostream>
    #include<cstdio>
    #include<fstream>
    #include<string>
    #include<QtGui>
    #include<QString>
    #include<QTextEdit>
    #include "bible2.h"
    #include "ui_bible2.h"

    using namespace std;

    bible2::bible2(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::bible2)

    {
    ui->setupUi(this);
    connect (ui->pushButton, SIGNAL(clicked()), this, SLOT(on_textEdit_textChanged()));
    }

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

    void bible2::on_pushButton_clicked()
    {
    ifstream in("example.csv");
    if(not in)
    {
    perror("example.csv");
    }
    else
    {
    string text;
    unsigned int numWords = 0;
    unsigned int revnumWords = 0;

            getline(in, text);
            cout << "Text: \"" << text << "\"" << endl;
            ui -> textEdit->append(text);
            //ui -> textEdit -> append(txt);
            for(int i=0; i < text.size(); i++)
            {
                    if(text[i] == ' ' || text[i] == '.' || text[i] == ',' ||
                       text[i] == '!' || text[i] == '?' || text[i] == ';')
                    {
                            ++numWords;
                            //cout << numWords + 10 << " ";
    
                            if (numWords > 8)
    
                                    {
                                            cout << numWords + 12 << " ";
                                            ui ->textEdit->append(QString::number(numWords+12));
    
                                    }
    
                            else
                                    {
                                            cout << numWords + 10 << " ";
                                            ui ->textEdit->append(QString::number(numWords+10));
                                    }
    
                    }
    
            }
            in.close();
            cout << "\n";
            cout << "In this textual file \"example.csv\" there exists " << numWords;
            cout << " words!"<< endl;
            ui -> textEdit->append("In this text file, there exists " + (QString::number(numWords)) + " words");
    
    
    }
    //return 0;
    

    }

    void bible2::on_textBrowser_historyChanged()
    {

    }

    void bible2::on_textEdit_textChanged()
    {
    //void QTextEdit::append(const QString& text);
    }
    @

    I keep getting an error : /home/ironmantis7x/Projects/Qt_code/bible2/bible2-build-desktop/../bible2/bible2.cpp:42: error: no matching function for call to ‘QTextEdit::append(std::string&)’

    Where am I going wrong? can someone please help me clear my confusion?

    Thanks in advance!

    ironmantis7x



  • The QTextEdit::append() method requires you to pass a QString object (Qt's equivalent of std::string). QString does not provide a constructor taking a std::string as an argument, so no implicit conversion can happen. You will have to use QString::fromStdString() instead.

    @
    ui->textEdit->append(QString::fromStdString(text));
    @

    Is there any reason you resort to the STL instead of using Qt for accessing and reading the file?



  • I am a newbie to c++ and Qt.
    I got the program to work in a console program and then I wanted to make it a GUI. So I dumped my code into Qt. I am still trying to get up to speed on Qt.
    Sorry to bother everyone with such a trivial question.

    Thanks for helping me with this. I can now finish up the project I was working on. This was my only sticking point.

    ironmantis7x



  • okay -- I definitely need to learn the Qt way of reading files.
    I just added a second line to my example.csv file and the program is only reading the 1st line in the file.

    Any ideas or suggestions?

    ironmantis7x



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



  • if you want to create a basic text editor then i will suggest you a tutorial
    http://devslabs.blogspot.com/2012/04/how-to-create-basic-text-editor-in-qt-c.html
    actually i have written that



  • Hey thanks guys!!
    I have one more how to question:

    Should I first count the number of lines (in a while loop) and then count the number of words so that my program will read the number of line first and then count the number of words? My thinking is that if I do it this way, it will count the lines first and then the words. when it comes to the last word, it will then move to the next line.

    Am I on the right track?

    ironmantis7x



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



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



  • 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



  • 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



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



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



  • You're welcome!



  • 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


Log in to reply
 

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