PDA

View Full Version : Problem finding text in a QTextEdit



Vash5556
16th May 2009, 03:14
I am trying to implement finding text in a TextEdit. I have written my own dialog to do this, and i got it to return the right string. Whenever i call find on the QTextEdit it never finds what im looking for. Here is my code for the find.



void VashTextEditor::find()
{
FindDialog findDialog(this);
if (!findDialog.exec())
return;

const QString searchString = findDialog.getSearchString();
bool test = documentEdit->find(searchString);

if (test)
{
QMessageBox::information(this, tr("Vash Text Editor"), tr("Expression found."));
}
else
{
QMessageBox::information(this, tr("Vash Text Editor"), tr("Expression not found."));
}
}

Here is the code for my FindDialog



#include <QtGui>

#include "finddialog.h"

FindDialog::FindDialog(QWidget *parent)
: QDialog(parent)
{
setupUi(this);

findButton->setEnabled(false);

connect(findButton, SIGNAL(clicked()), this, SLOT(accept()));
connect(cancelButton, SIGNAL(clicked()), this, SLOT(reject()));
connect(findLineEdit, SIGNAL(textChanged(const QString &)), this, SLOT(toggleFindButton()));

}

QString FindDialog::getSearchString()
{
return findLineEdit->text();
}

void FindDialog::toggleFindButton()
{
QString currentText = findLineEdit->text();

if (!currentText.isEmpty())
{
findButton->setEnabled(true);
}
else
{
findButton->setEnabled(false);
}
}


Maybe im missing something simple. Any help would be appreciated. And if more code is needed feel free to ask and I will gladly post it for you.

kichi
16th May 2009, 07:13
Perhaps, you need to replace the entered string with a string that includes "regular expression".

For example, if you specify the string "^.*hello.*$", you can find "hello".

Vash5556
16th May 2009, 15:26
Perhaps, you need to replace the entered string with a string that includes "regular expression".

For example, if you specify the string "^.*hello.*$", you can find "hello".

Thanks for the suggestion. I tried this out and I am still getting the same behavior. Any suggestions?

Lykurg
16th May 2009, 16:55
Hi,



void VashTextEditor::find()
{
FindDialog findDialog(this);
if (!findDialog.exec())
return;

const QString searchString = findDialog.getSearchString();
bool test = documentEdit->find(searchString);

if (test)
{
QMessageBox::information(this, tr("Vash Text Editor"), tr("Expression found."));
}
else
{
QMessageBox::information(this, tr("Vash Text Editor"), tr("Expression not found."));
}
}

Seems fine, but to debug what does following code produce (at the end of your function)?



qWarning() << searchString;
qWarning() << documentEdit->toPlainText();
qWarning() << documentEdit->textCursor()->position();








void FindDialog::toggleFindButton()
{
QString currentText = findLineEdit->text();

if (!currentText.isEmpty())
{
findButton->setEnabled(true);
}
else
{
findButton->setEnabled(false);
}
}



findButton->setEnabled(!findLineEdit->text().isEmpty());does the same.

P.s.: Expected is a normal string, no regular expression.

Vash5556
16th May 2009, 17:23
Hi,


Seems fine, but to debug what does following code produce (at the end of your function)?



qWarning() << searchString;
qWarning() << documentEdit->toPlainText();
qWarning() << documentEdit->textCursor()->position();




After adding the qWarning calls, this is what i get for output.



$ ./VashTextEditor
"test" <-- qWarning() << searchString
"test" <-- qWarning() << documentEdit->toPlainText()


So it looks like it is returning the right value from my dialog, and my document edit is showing that the text is there, but it is still returning false on my find.

Lykurg
16th May 2009, 17:32
and what is
qWarning() << documentEdit->textCursor()->position(); saying? before opening your search dialag make sure that the text cursor is at the front of the text. If then still is false return please make a minimal compilable example and post it.

Lykurg
16th May 2009, 17:35
if it is still not working you may want use the advanced option of QTextDocument. (-> QTextEdit::document())

kichi
16th May 2009, 20:10
Originally Posted by Lykurg >
P.s.: Expected is a normal string, no regular expression.

I'm sorry.
My suggestion was wrong.


I made a sample that uses QTextEdit::find().
It works fine.

Vash5556
18th May 2009, 15:19
and what is
qWarning() << documentEdit->textCursor()->position(); saying? before opening your search dialag make sure that the text cursor is at the front of the text. If then still is false return please make a minimal compilable example and post it.

Thanks so much for that, I didn't even think to check about where the text cursor was. It is working now. Thanks a bunch to everyone who helped.