PDA

View Full Version : Text search utility



NewGuy
22nd June 2006, 17:54
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


QStringList::Iterator it = FilePath.begin(); //FilePath holds the filepaths
QTextStream TextStream;
QString Line;
if (MsgBoxAnswer)
{
while(it != FilePath.end())
{
ui.txtOutput->append("Before TextStream"); //Test to see whether the program works so far
TextStream << *it;
while(!TextStream.atEnd())
{
ui.txtOutput->append("in TextStream"); //Test to see whether the program works so far
Line = TextStream.readLine(0);
ui.txtOutput->append(Line);
}
++it;
}
}

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

wysota
22nd June 2006, 19:47
You have to "attach" the stream to some QIODevice (like QFile) to use it.

NewGuy
11th July 2006, 19:12
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.

wysota
16th July 2006, 00:17
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:


QFile f("myfile");
f.open(QIODevice::ReadOnly);
textedit->setDocument(new QTextDocument(f.readAll()));

NewGuy
16th July 2006, 16:40
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

wysota
16th July 2006, 22:45
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).


QFile files[5];
for(int i=0;i<5;i++){
//...
files[i].open(QIODevice::ReadOnly);
}

QTextDocument document;
QTextCursor cursor(&document);
for(int i=0;i<5;i++){
cursor.movePosition(QTextCursor::End);
cursor.insertText(files[i].readAll());
}

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().

NewGuy
23rd July 2006, 11:47
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?

wysota
23rd July 2006, 11:59
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)?