Results 1 to 17 of 17

Thread: Extracting text from files (AKA I'm a newbie!)

  1. #1
    Join Date
    Jan 2009
    Posts
    21
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default Extracting text from files (AKA I'm a newbie!)

    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

    Qt Code:
    1. void myQtApp::read()
    2. {
    3. char buffer [256];
    4. ifstream myfile ("C:test.txt");
    5. while (!myfile.eof())
    6. {
    7. myfile.getline (buffer,256);
    8. QMessageBox::about(this,"About myQtApp",buffer);
    9. }
    10. }
    To copy to clipboard, switch view to plain text mode 
    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.
    Last edited by jpn; 22nd January 2009 at 17:55. Reason: missing [code] tags

  2. #2
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Extracting text from files (AKA I'm a newbie!)

    See QFile and QTextStream. Make sure to read through the detailed description of each class.
    J-P Nurmi

  3. The following user says thank you to jpn for this useful post:

    mike phillips (26th January 2009)

  4. #3
    Join Date
    Jan 2009
    Posts
    21
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Extracting text from files (AKA I'm a newbie!)

    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!

  5. #4
    Join Date
    Dec 2008
    Posts
    13
    Thanks
    2
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Extracting text from files (AKA I'm a newbie!)

    From http://doc.trolltech.com/4.4/qfile.html -->

    Reading Files Directly

    The following example reads a text file line by line:
    Qt Code:
    1. QFile file("in.txt");
    2. if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
    3. return;
    4.  
    5. while (!file.atEnd()) {
    6. QByteArray line = file.readLine();
    7. process_line(line);
    8. }
    To copy to clipboard, switch view to plain text mode 
    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:
    Qt Code:
    1. QFile file("in.txt");
    2. if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
    3. return;
    4.  
    5. QTextStream in(&file);
    6. while (!in.atEnd()) {
    7. QString line = in.readLine();
    8. process_line(line);
    9. }
    To copy to clipboard, switch view to plain text mode 

  6. #5
    Join Date
    Jan 2009
    Posts
    21
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Extracting text from files (AKA I'm a newbie!)

    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. What is your solution please?

  7. #6
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Extracting text from files (AKA I'm a newbie!)

    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.
    J-P Nurmi

  8. #7
    Join Date
    Jan 2009
    Posts
    21
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Extracting text from files (AKA I'm a newbie!)

    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.

  9. #8
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Extracting text from files (AKA I'm a newbie!)

    Quote Originally Posted by mike phillips View Post
    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.
    J-P Nurmi

  10. #9
    Join Date
    Jan 2009
    Posts
    21
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Extracting text from files (AKA I'm a newbie!)

    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.

  11. #10
    Join Date
    Jan 2009
    Posts
    21
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Extracting text from files (AKA I'm a newbie!)

    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!

  12. #11
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Extracting text from files (AKA I'm a newbie!)

    Quote Originally Posted by mike phillips View Post
    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":
    Qt Code:
    1. QString text = lineEdit->text();
    To copy to clipboard, switch view to plain text mode 

    Quote Originally Posted by mike phillips View Post
    I find the lack of code examples in QT compared to C and C++ a handicap!
    Not to be rude, but have you seen

    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.
    J-P Nurmi

  13. The following user says thank you to jpn for this useful post:

    mike phillips (26th January 2009)

  14. #12
    Join Date
    Jan 2009
    Posts
    21
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Extracting text from files (AKA I'm a newbie!)

    Quote Originally Posted by JPN
    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.

  15. #13
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Extracting text from files (AKA I'm a newbie!)

    Ok, I'm glad the problem was eventually solved.

    Quote Originally Posted by mike phillips View Post
    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.
    J-P Nurmi

  16. #14
    Join Date
    Jan 2009
    Posts
    21
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Extracting text from files (AKA I'm a newbie!)

    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'?

  17. #15
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Extracting text from files (AKA I'm a newbie!)

    you nee implement process_line it's just an example.
    for exmaple process_line can look like
    Qt Code:
    1. void MyWidget::process_line(const QString &line)
    2. {
    3. qDebug() << line;
    4. //here we should process a line
    5. }
    To copy to clipboard, switch view to plain text mode 
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  18. The following user says thank you to spirit for this useful post:

    mike phillips (30th January 2009)

  19. #16
    Join Date
    Jan 2009
    Posts
    21
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Extracting text from files (AKA I'm a newbie!)

    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!

  20. #17
    Join Date
    Oct 2008
    Location
    Europe
    Posts
    37
    Thanks
    3
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Unhappy Re: Extracting text from files (AKA I'm a newbie!)

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

Similar Threads

  1. Unhandled exception in qatomic
    By NewGuy in forum Qt Programming
    Replies: 14
    Last Post: 23rd July 2013, 09:49
  2. Access violation when reading big text files
    By robertson1 in forum General Programming
    Replies: 0
    Last Post: 18th September 2008, 06:59
  3. problem with reading text files
    By Axsis in forum Newbie
    Replies: 1
    Last Post: 25th April 2008, 12:29
  4. Handling mac text format files - support?
    By bnilsson in forum Qt Programming
    Replies: 0
    Last Post: 29th March 2008, 12:10
  5. how to save sequences of text files and sound files
    By nagpalma in forum Qt Programming
    Replies: 8
    Last Post: 3rd July 2007, 00:06

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.