PDA

View Full Version : How to display text from a file to a text browser



ironmantis7x
11th June 2012, 11:29
Hi all!
I am attempting to read text from a .csv file and then count the umber of words and display the line and then word count. I can get it to work in a console application in c++ (n linux) but when I put that same code into Qt -- it won't display in the object browser.

Here is my code below:


#include<iostream>
#include<cstdio>
#include<fstream>
#include<string>
#include "bible.h"
#include "ui_bible.h"

unsigned int numWords = 0;

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

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

void bible::on_comboBox_activated(const QString &arg1)
{

}

void bible::on_pushButton_clicked()
{
std::ifstream in("example.csv");
if(not in)
{
perror("example.csv");
}
else
{
std::string text;
//unsigned int numWords = 0;
//unsigned int revnumWords = text.size();
getline(in, text);
std::cout << "Text: \"" << text << "\"" << std::endl;
for(int i=0; i < text.size(); i++)
{
if(text[i] == ' ' || text[i] == '.' || text[i] == ',' ||
text[i] == '!' || text[i] == '?' || text[i] == ';')
{
++numWords;
//--revnumWords;
//cout << revnumWords << " ";

if (numWords > 8)

{
std::cout << numWords + 12 << " ";
//cout << revnumWords << " ";

}

else
{
std::cout << numWords + 10 << " ";
//cout << revnumWords << " ";
}

}

}

}

}


void bible::on_textBrowser_historyChanged()
{
if (numWords > 8)

{
std::cout << numWords + 12 << " ";
//cout << revnumWords << " ";

}

else
{
std::cout << numWords + 10 << " ";
//cout << revnumWords << " ";
}

}


Any help would be greatly appreciated.

ironmantis7x

wysota
11th June 2012, 12:02
You don't have any code related to displaying anything anywhere in your GUI.

ironmantis7x
11th June 2012, 14:45
You don't have any code related to displaying anything anywhere in your GUI.

Can you help me understand where that code goes and what it looks like? I am really lost here...

ironmantis7x

wysota
11th June 2012, 16:21
You want me to teach you C++?

ironmantis7x
11th June 2012, 16:39
You want me to teach you C++?

I please ask that you do not be rude or condescending to me. I am asking because I only know basic c++ and the GUI stuff I learned was on Visual Studio 2010 and I really want to learn Qt way of doing things so I can stay on Linux.

wysota
11th June 2012, 16:48
I am not rude, I am merely asking, what you want me (or rather us all) to do? You didn't say what you tried, what didn't work. You didn't say what doubts you had, what you didn't understand from the documentation you had read. If you ask specific questions, you'll get specific answers. If you ask general questions then either you will get general answers or no answers at all.

ironmantis7x
11th June 2012, 16:53
I am not asking anyone to write code for me... that is not my aim.
I read the docs and I am just confused on how to link the output of reading the test file to the correct slot for the QTextBrowser. That is my question. I learn by example best of all. SO what I am asking for is an example of how to link out put (any output) to a QTextBrowser widget.

ironmantis7x

wysota
11th June 2012, 18:24
Qt is C++. In Qt you can use anything C++ allows, thus you can call any public method of any public class you want. Including QTextEdit::setPlainText() or QTextEdit::setHtml().

ironmantis7x
11th June 2012, 18:37
thanks for setting me straight on the documentation. I will dive into this and if I have any further questions, I will ask.

ironmantis7x

ironmantis7x
14th June 2012, 00:10
Okay -- after some reading and coding, I cam up with the following:



#include<iostream>
#include<cstdio>
#include<fstream>
#include<string>
#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;
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 << " ";

}

else
{
cout << numWords + 10 << " ";
}

}

}
in.close();
cout << "\n";
cout << "In this textual file \"example.csv\" there exists " << numWords;
cout << " words!"<< endl;

}
//return 0;
}

void bible2::on_textBrowser_historyChanged()
{

}

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

I am STILL not getting the text to show up in the GUI window.

What is wrong with my append statement?

ironmantis7x

ChrisW67
14th June 2012, 01:13
Please use
and tags around code.



I am STILL not getting the text to show up in the GUI window.

What is wrong with my append statement?




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

This is basic C++ knowledge and nothing to do with Qt.

Line 3 is not a call to QTextEdit::append(), it is a declaration that such a function exists and takes a QString as a parameter. My compiler refuses to compile that and yours probably does too. You need to call the function append() on an instance of the QTextEdit class, i.e. an object. There's a QTextEdit object representing the on-screen text editor inside the ui object, so you can use the ui pointer to access it:


ui->textEdit->append("some text");

You are still going to have to put that call in the correct place; I suggest that where you have it is completely nonsensical. Think about where the output from your console program was emitted...

ironmantis7x
14th June 2012, 15:23
This was awesome!! Thanks for pointing out to me where I went wrong. I kept thinking it was a QT thing when clearly I have a lot more to learn about c++! I am a newbie to c++ and Qt.

Thanks for helping me out. my program is starting to work correctly now.

ironmantis7x