PDA

View Full Version : Thread Search in text file large



marcos.miranda
11th June 2012, 04:01
Hi everyone. :)
I need a system that searches of large text files, you must have a main form and a search form that runs on a different thread because it freezes the system. I must also open up new forms of search that works simultaneously and I can stop, pause and restart the search.

Main Form - frmTelaPrincipal.cpp

void clsTelaPrincipal::on_actionCadastro_de_Clientes_tr iggered()
{
clsCadClientes *objfrmCadClientes = new clsCadClientes;
ui->mdiArea->addSubWindow(objfrmCadClientes);
objfrmCadClientes->show();

Search Form - frmCadClientes.cpp

void clsCadClientes::on_cmdTextEdit_clicked()
{
Open file routine here...
ui->txtEdit->setPlainText(in.readAll());
file.close();
}
}

void clsCadClientes::on_cmdSearch_clicked()
{
while (ui->txtEdit->find(ui->lineEdit->text()))
{
ui->txtEdit->setTextBackgroundColor("yellow");
}

How do I post the source code?

marcos.miranda
msn: jmbm.trab@bol.com.br

wysota
11th June 2012, 07:52
And what do you want us to do with what you posted?

marcos.miranda
11th June 2012, 14:59
Hi, wysota
As you may have gathered, I am a beginner in QT Framework and C + +, I'm lost with this new language. Thanks for responding.
First explain to me how do I post to blocks of text like you did.
QT Code:

Second
Regarding your question and I posted, as I open the form (Search Form - frmCadClientes.cpp) in another thread and it works without freezing the main form and other forms open.

Posted the source code to facilitate for those who help me and I understand when the solver put it back in response to the problem.

Thank you for your attention.

wysota
11th June 2012, 16:20
First explain to me how do I post to blocks of text like you did.
You place
and tags around your code.


Regarding your question and I posted, as I open the form (Search Form - frmCadClientes.cpp) in another thread and it works without freezing the main form and other forms open.
You can't access widgets from within worker threads. This will crash your program.


Posted the source code to facilitate for those who help me and I understand when the solver put it back in response to the problem.
The thing is you didn't say what the problem was.

marcos.miranda
11th June 2012, 21:17
Hi,

Frist problem

When I press the Open File button and select the text file into a string search the system freezes. The code executed is the following:



QString fileName = QFileDialog::getOpenFileName(this, tr("Open File"), "",
tr("Text Files (*.txt);;MSWord Files (*.doc)"));
if (fileName != "") {
QFile file(fileName);
if (!file.open(QIODevice::ReadOnly | QIODevice::Text) ) {
QMessageBox::critical(this, tr("Error"), tr("Could not open file"));
return;
}

QTextStream in(&file);
ui->txtEdit->setPlainText(in.readAll());
file.close();


How can this not happen?


Second problem

When I start a search for a particular string in the text file back to the system freeze to end the search. The code executed is the following:



int vnumLinhaInit,vnumLinhasTotal;

vnumLinhaInit=0;
vnumLinhasTotal = ui->txtEdit->document()->lineCount();
ui->txtEdit->moveCursor(QTextCursor::Start);

while (ui->txtEdit->find(ui->lineEdit->text()))
{
ui->txtEdit->setTextBackgroundColor("yellow");
ui->lblContador->setText(QString::number( vnumLinhaInit ));
vnumLinhaInit += 1;
}

QMessageBox msgBox;
msgBox.setText("A Busca Terminou !!!");
msgBox.exec();


How can this not happen?


As I understand my problem all forms are running in the same thread and have a heavy processing in both the opening of the text file as the moment of the search string in this large file system freezes. following drawing:

7850


It could be done the following?

Run the search forms on different threads as in the main drawing below, so that the search processing can be without interfering with each other

7851

How do? I do not have no idea!
If you or anyone has a solution to this freezing does not happen please post the code.

Thank you for your attention.

wysota
11th June 2012, 21:45
When I press the Open File button and select the text file into a string search the system freezes.
The system or your program?



When I start a search for a particular string in the text file back to the system freeze to end the search.
If by "the system" you mean your application, then it's quite normal since it takes some time for the while loop to execute.


How do? I do not have no idea!
You can do the search in an external thread (but not on the QTextEdit object!!!) or you can let the application process events (using QCoreApplication::processEvents()) from time to time while searching. Note that probably there are faster algorithms than using QTextEdit::find().

marcos.miranda
11th June 2012, 22:02
Hi, wysota

QCoreApplication :: processEvents (), I used inside the while loop has mitigated the problem, because i managed to open another form of search and start a new search simultaneously, was when I realized that this new form stopped interfering in the search form above to search old already open.

Is it possible when I call a new form of search it opens in a separate process of the program? How?



Thanks....

wysota
11th June 2012, 22:24
I don't really understand what you mean. The term "process" has a concrete meaning when it comes to computers, I don't think you really meant that.

marcos.miranda
11th June 2012, 22:53
Hi, wysota

Is it possible when I call a new form of search it opens in a separate Thread ?
How?



Thanks....

wysota
12th June 2012, 00:04
No, forms don't open "in threads". You can do the search in a separate thread and display the results in a window.

marcos.miranda
12th June 2012, 15:22
Hi everyone.

The QFile can open a file type. "Rtf and. Doc"?
How would the code if you can?


Thanks..

wysota
12th June 2012, 20:54
A file is a file. QFile doesn't care about content of the file you use. If you want to ask whether you can import an rtf or doc document to QTextEdit, then no, you'd have to have a dedicated import component.