Results 1 to 8 of 8

Thread: Text search utility

  1. #1
    Join Date
    Jun 2006
    Posts
    19
    Thanks
    4
    Qt products
    Qt4

    Default Text search utility

    I have started to teach myself Qt. I have, in the lack of a better assignment, given myself the task to wirte a small program that searches several text files for a specific phrase. So far I've run into a small problem with QTextStream. The idea is that the program asks the user if he wishes to view the files being searched in a TextEdit in the mainwindow. I just can't seem to get it done right

    Qt Code:
    1. QStringList::Iterator it = FilePath.begin(); //FilePath holds the filepaths
    2. QTextStream TextStream;
    3. QString Line;
    4. if (MsgBoxAnswer)
    5. {
    6. while(it != FilePath.end())
    7. {
    8. ui.txtOutput->append("Before TextStream"); //Test to see whether the program works so far
    9. TextStream << *it;
    10. while(!TextStream.atEnd())
    11. {
    12. ui.txtOutput->append("in TextStream"); //Test to see whether the program works so far
    13. Line = TextStream.readLine(0);
    14. ui.txtOutput->append(Line);
    15. }
    16. ++it;
    17. }
    18. }
    To copy to clipboard, switch view to plain text mode 

    What is the matter? The program never enters the while(!TextStream.atEnd()) loop

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Text search utility

    You have to "attach" the stream to some QIODevice (like QFile) to use it.

  3. #3
    Join Date
    Jun 2006
    Posts
    19
    Thanks
    4
    Qt products
    Qt4

    Default Re: Text search utility

    I got it to work, thanks.
    I have a new problem however. If the user wishes to view several large files it takes forever for QTextEdit to display the files - undoubtly because I chop the text into thousands of strings and then append them. How do I optimize the code to run faster? There must a faster way.

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Text search utility

    Use QTextDocument and make sure the text is formatted into paragraphs, as QTextEdit is optimised for working with paragraphs. Why do you chop the file lines into separate strings?

    How about doing something like:

    Qt Code:
    1. QFile f("myfile");
    2. f.open(QIODevice::ReadOnly);
    3. textedit->setDocument(new QTextDocument(f.readAll()));
    To copy to clipboard, switch view to plain text mode 

  5. #5
    Join Date
    Jun 2006
    Posts
    19
    Thanks
    4
    Qt products
    Qt4

    Default Re: Text search utility

    The problem with textedit->setDocument is that it overwrites the current text in the text edit so I can't view multiple files at one time

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Text search utility

    You want to merge files? So merge them on the document level and not on the text edit level (and definitely don't split them into lines).

    Qt Code:
    1. QFile files[5];
    2. for(int i=0;i<5;i++){
    3. //...
    4. files[i].open(QIODevice::ReadOnly);
    5. }
    6.  
    7. QTextDocument document;
    8. QTextCursor cursor(&document);
    9. for(int i=0;i<5;i++){
    10. cursor.movePosition(QTextCursor::End);
    11. cursor.insertText(files[i].readAll());
    12. }
    To copy to clipboard, switch view to plain text mode 

    You don't have to set a new document every time you wish to add a new file, you can operate on the existing one which you can retrieve using QTextEdit::document().

  7. The following user says thank you to wysota for this useful post:

    NewGuy (23rd July 2006)

  8. #7
    Join Date
    Jun 2006
    Posts
    19
    Thanks
    4
    Qt products
    Qt4

    Default Re: Text search utility

    Thanks for the help - I simply added the documents as tabs to my mainwindow.

    I have a new problem however (yes - I'm still learning). I wish to use the Boyer-Moore string searching algorithm for my search function. I think I can write this myself. Thing is, I need to have something to search in. I am not sure if it's best to use a QByteArray, QBuffer or simply a Qstring. The thought is that the program should be able to handle very large text files (500 MBs perhaps). Which would be the most efficient for this?

  9. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Text search utility

    First I'd make tests if BM algorithm will speed the search. I don't know what does Qt use, but even if this is simply strstr(), it's a very efficient function.

    As for the datatype, why not operate on QFile directly (or QTextDocument, if you have one)?

Similar Threads

  1. widget for text AND buttons
    By nongentesimus in forum Newbie
    Replies: 2
    Last Post: 16th June 2006, 14:43
  2. Finding text on Text edit
    By jyoti kumar in forum Qt Programming
    Replies: 2
    Last Post: 18th May 2006, 14:20
  3. QTextEdit API questions (plain text)
    By Gaspar in forum Qt Programming
    Replies: 4
    Last Post: 16th May 2006, 07:03

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.