PDA

View Full Version : Extracting text from files (AKA I'm a newbie!)



mike phillips
22nd January 2009, 17:42
Complete beginner. Just starting with QT Creator and I wish to extract text from text files and do 'things' with it.

So far I am easy with C++ and can get the text file into cout. I can also access the lines sequentially into a Qmessagebox on my form using


void myQtApp::read()
{
char buffer [256];
ifstream myfile ("C:test.txt");
while (!myfile.eof())
{
myfile.getline (buffer,256);
QMessageBox::about(this,"About myQtApp",buffer);
}
}

which I have 'cobbled' together from various on-line sources

As a starter I wish to display the file contents in a textEdit box 'textEdit_2' on my form but cannot find a suitable form of words which will compile without some sort of error.

I would be most grateful for some light in the dark, and any pointers to a comprehensive online 'beginner's guide' to QT.

jpn
22nd January 2009, 17:46
See QFile and QTextStream. Make sure to read through the detailed description of each class.

mike phillips
22nd January 2009, 21:48
I appreciate the fast response, JPN - I had already read those but unfortunately found very little to help me at my stage of (lack of) 'understanding'. A hint of code would be very useful!

kazek3018
22nd January 2009, 22:58
From http://doc.trolltech.com/4.4/qfile.html -->

Reading Files Directly

The following example reads a text file line by line:


QFile file("in.txt");
if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
return;

while (!file.atEnd()) {
QByteArray line = file.readLine();
process_line(line);
}

The QIODevice::Text flag passed to open() tells Qt to convert Windows-style line terminators ("\r\n") into C++-style terminators ("\n"). By default, QFile assumes binary, i.e. it doesn't perform any conversion on the bytes stored in the file.
Using Streams to Read Files

The next example uses QTextStream to read a text file line by line:


QFile file("in.txt");
if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
return;

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

mike phillips
23rd January 2009, 19:52
kazec - I have seen that one too. Problem using that is I get 'process_line was not declared in this scope'. I cannot find ANTHING to allow me to correct this compile error. 2 steps forward and 1 back.:confused: What is your solution please?

jpn
23rd January 2009, 19:55
It's an example function call to a function that would process a single line of text. You can replace it with your message box code if you want to see each line in a message box, one by one. Don't copy-paste code snippets blindly but try to think what they actually do. :)

mike phillips
23rd January 2009, 22:42
As I said at post #1, I can already read the file in a message box. I need to be able to read it into a text file and am trying to get it to append to a textbox for starters. What I am looking for is some clues as to the code to follow from line #7 in my paste so I can slot it into the cpp.

jpn
24th January 2009, 10:27
As I said at post #1, I can already read the file in a message box.
Yes, and I proposed improving your code by switching to QFile/QTextStream.


I need to be able to read it into a text file and am trying to get it to append to a textbox for starters. What I am looking for is some clues as to the code to follow from line #7 in my paste so I can slot it into the cpp.
So what is the problem? A text box in Qt terms would be QTextEdit. So replace "process_line(line);" with "textEdit->append(line);" where "textEdit" is a pointer to QTextEdit.

mike phillips
24th January 2009, 14:46
So what is the problem? - the problem was, as stated, that I am a complete 'newbie', hence my presence here in this forum. HOWEVER, thank you for turning on the light on this one. I now have a textbox with the file contents, so, onwards and upwards. Up until 22 Jan no matter what I was adding to my code to fill a textbox it was refusing to compile.

mike phillips
26th January 2009, 09:09
Well, I have 'moved on' significantly. However, (for JPN - yes. I have spent some hours looking at trolletch's pages:) ) whilst I am reasonably OK with C++ techniques on strings, it is the widget side of things I cannot fathom - how I get the contents of, say, a lineEdit box into a named string to process. Every avenue I explore returns a complile error.

A clue as to which property/function/method I need to address would be appreciated - eg is it copy and paste or is it a case of signal/slot?

I find the lack of code examples in QT compared to C and C++ a handicap!

jpn
26th January 2009, 09:32
how I get the contents of, say, a lineEdit box into a named string to process.
Provided that you have a pointer to QLineEdit, "lineEdit":

QString text = lineEdit->text();


I find the lack of code examples in QT compared to C and C++ a handicap!
Not to be rude, but have you seen

How to Learn Qt
Tutorials
Qt Examples

In my opinion the amount of example snippets in Qt docs is quite fine. It makes no sense to put a code snippet to each and every function, of course. Most of the code snippets can be found in the detailed description. You should always read the detailed description before starting to use a class you're not familiar with. And as mentioned by "How to Learn Qt" -page, the documentation expects you to have basic C++ knowledge. Teaching C++ basics is out of Qt reference documentation's scope.

mike phillips
26th January 2009, 12:47
the documentation expects you to have basic C++ knowledge. Teaching C++ basics is out of Qt reference documentation's scope. - indeed thanks again (problem solved), but I thought I had covered that bit in posts #1 and '#10? - no offence taken incidentally. I am privileged to be answered by a 'guru' indeed - I had assumed I would be 'aided' by another 'newbie' who was further along the liearning path than I. It is the widget stuff I am finding difficulty with, not C or C++. I could easily do what I was trying to do in both Visual Basic and Visual C++ but not QT (assuming I am allowed to mention those here:)).

Re your reference to "Most of the code snippets can be found in the detailed description." I had explored all of those links you listed already, but found no easy clues (a 'newbie' you recall and hence my presence in the QT 'newbie' forum) as to whether I should be working on the QLineEdit widget, the QString widget, the QTextEdit, the QTextStream or setting up a signal/slot or what. Maybe I am looking at the wrong "detailed descriptions."?

If I am in the wrong forum/place, I need to be told - I moderated on an aviation related Jelsoft/vBulletin forum for some time, so am not averse to being 'modded'. If, indeed, you know of another more suitable forum for absolute beginners in QT please let me know.

jpn
26th January 2009, 13:45
Ok, I'm glad the problem was eventually solved.


Re your reference to "Most of the code snippets can be found in the detailed description." I had explored all of those links you listed already, but found no easy clues (a 'newbie' you recall and hence my presence in the QT 'newbie' forum) as to whether I should be working on the QLineEdit widget, the QString widget, the QTextEdit, the QTextStream or setting up a signal/slot or what. Maybe I am looking at the wrong "detailed descriptions."?
Well, I don't know what to say. Such low-level things as QString, QFile or QTextStream belong to the QtCore module and have nothing to do with GUI. On the other hand, widget classes such as QLineEdit and QTextEdit that can be found from the QtGui module, usually have something to do with GUI... Anyway, take your time to get comfortable with Qt, I'm sure you'll like it.

Oh, and just in case you didn't notice the tool called Qt Assistant yet, I really recommend checking it out. It's an excellent little tool to explore Qt reference docs. Especially the Index-tab is worth trying out.


If I am in the wrong forum/place, I need to be told - I moderated on an aviation related Jelsoft/vBulletin forum for some time, so am not averse to being 'modded'. If, indeed, you know of another more suitable forum for absolute beginners in QT please let me know.
This is the correct place. I hope I didn't drive you away. :)

PS. One more thing. It's Qt, not QT. The latter refers to Apple's QuickTime.

mike phillips
29th January 2009, 12:33
Qt noted.

Tinfoil hat on again...

One more thing I am stuck on, and pointers to help docs appreciated - believe me I have tried looking. Nothing in 'assistant'.

The example cut and pasted in post#4 above (from your help files) contains "process_line(line);". This will not compile - 'process_line not delared in this scope' . I can find NO reference to process_line in C++ or Qt. Am I missing some '#include'?

spirit
29th January 2009, 13:04
you nee implement process_line it's just an example.
for exmaple process_line can look like


void MyWidget::process_line(const QString &line)
{
qDebug() << line;
//here we should process a line
}

mike phillips
30th January 2009, 08:48
Thank you, s - I gather you are saying it is just a call to a fictionally named void? That means a lot of time wasted Googling.... Alles claar!

georgep
30th January 2009, 17:27
I am also a noob, and while the documentation is great for nonbeginners, I often find myself googling for simple working code snippets.
I really think Qt needs a lot more simple code snippets exactly for newbies like me. Also small working code snippets get a noobs spirits up.

ps: I am not talking about c++