PDA

View Full Version : How to read a textfile and display result in gui [Qt programming problem]



alwaysTry
17th September 2010, 19:04
could anyone give a helping hand?
my code was able to read the textfile and display using gui.
the textfile "data.txt" is in this form

123-GCF 2 1 2
124-BOG 4 1 2 5 7
456-DES 6 6 7 8 9 3 1
234-REE 2 9 8

wherein :
123-GCF = code
2 = size of array
1 2 = array of numbers

i need to count the frequency of numbers in the third column that is the array of numbers
and display the result in the gui.

in this example once i clicked the display button it should show
1 = 3;
2 = 2;
3 = 1;
...
...
...

so far I have this code................... your help is very much appreciated..

thanks in advance ....



#include<QtCore>
#include<QLabel>
#include<QFile>
#include<QtGui>
#include<QString>
#include <iostream>
class DblVec : public QVector<int>{
public:
DblVec(int n):QVector<int>(n){}
};
using namespace std;

int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QWidget *window = new QWidget;
QGridLayout *layout = new QGridLayout;
QPushButton *readFile = new QPushButton;
QString date;
quint32 size;
DblVec line(size);
QTextBrowser *browse = new QTextBrowser;
QScrollBar *scrollbar = new QScrollBar;
QFile infile("data.txt");

window->setWindowTitle("Data");
readFile->setText("Display Relative Frequency");
layout->addWidget(readFile, 0, 1);

if ( infile.open(QIODevice::ReadOnly |QIODevice::Text) )
{QString line2;
QTextStream stream( &infile );
while(!stream.atEnd())
{
line2 = stream.readAll();
}

browse->setText(line2);
browse->setVerticalScrollBar(scrollbar);
browse->show();
infile.close();
}

layout->addWidget(browse, 0, 0);
window->setLayout(layout);
window->resize(500, 400);
window->show();
return a.exec();
}

tbscope
17th September 2010, 19:17
Counting numbers is pretty easy.

Instead of giving you the answer to your homework, try to think about the problem yourself.

What would you do to count the numbers?

Edit: lets make this simple.
I have: 1 1 2 3 5 2 8 1
How many times does 2 appear in this list? How do you count it?

alwaysTry
17th September 2010, 19:43
sorry, my englsih is very bad. i think i am not understood very well here.

my code shows that i am using QString for the entire line, that is how i read the textfile.

i am new to Qt so i am not even familiar with all the libraries and how to use it.

at first i tried to assign Qstring for the code, quint32 for the size, QVector for the array
but its still not working.

my problem is accessing the third column. obviously, i can't acces third column if i assign QString for the entire line when reading the textfile. I need to find ways of reading wherein I can access the third column. And from there, i can start doing my counting.

thanks for the comment... i will try to figure it out myself. it's hard to explain my english is very bad. a lesson is learn today. don't ever ask or you shall be bad mouthed.

tbscope
17th September 2010, 19:50
Ohh, from your first question I thought you had a problem with counting.

For the parsing, use a QTextstream but do not read the entire line into a string.
Try something like


QString string;
int integer1;
...

stream >> string >> integer1 >> ...;

wysota
17th September 2010, 20:18
You can also use QString::split() to get a list of field values but then you'll have to convert them to the types you need.