Results 1 to 9 of 9

Thread: Problem finding text in a QTextEdit

  1. #1
    Join Date
    Sep 2006
    Posts
    13
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Problem finding text in a QTextEdit

    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.

    Qt Code:
    1. void VashTextEditor::find()
    2. {
    3. FindDialog findDialog(this);
    4. if (!findDialog.exec())
    5. return;
    6.  
    7. const QString searchString = findDialog.getSearchString();
    8. bool test = documentEdit->find(searchString);
    9.  
    10. if (test)
    11. {
    12. QMessageBox::information(this, tr("Vash Text Editor"), tr("Expression found."));
    13. }
    14. else
    15. {
    16. QMessageBox::information(this, tr("Vash Text Editor"), tr("Expression not found."));
    17. }
    18. }
    To copy to clipboard, switch view to plain text mode 

    Here is the code for my FindDialog

    Qt Code:
    1. #include <QtGui>
    2.  
    3. #include "finddialog.h"
    4.  
    5. FindDialog::FindDialog(QWidget *parent)
    6. : QDialog(parent)
    7. {
    8. setupUi(this);
    9.  
    10. findButton->setEnabled(false);
    11.  
    12. connect(findButton, SIGNAL(clicked()), this, SLOT(accept()));
    13. connect(cancelButton, SIGNAL(clicked()), this, SLOT(reject()));
    14. connect(findLineEdit, SIGNAL(textChanged(const QString &)), this, SLOT(toggleFindButton()));
    15.  
    16. }
    17.  
    18. QString FindDialog::getSearchString()
    19. {
    20. return findLineEdit->text();
    21. }
    22.  
    23. void FindDialog::toggleFindButton()
    24. {
    25. QString currentText = findLineEdit->text();
    26.  
    27. if (!currentText.isEmpty())
    28. {
    29. findButton->setEnabled(true);
    30. }
    31. else
    32. {
    33. findButton->setEnabled(false);
    34. }
    35. }
    To copy to clipboard, switch view to plain text mode 

    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.

  2. #2
    Join Date
    Nov 2007
    Posts
    31
    Thanks
    2
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Post Re: Problem finding text in a QTextEdit

    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".
    kichi

  3. #3
    Join Date
    Sep 2006
    Posts
    13
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Problem finding text in a QTextEdit

    Quote Originally Posted by kichi View Post
    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?

  4. #4
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Problem finding text in a QTextEdit

    Hi,
    Quote Originally Posted by Vash5556 View Post
    Qt Code:
    1. void VashTextEditor::find()
    2. {
    3. FindDialog findDialog(this);
    4. if (!findDialog.exec())
    5. return;
    6.  
    7. const QString searchString = findDialog.getSearchString();
    8. bool test = documentEdit->find(searchString);
    9.  
    10. if (test)
    11. {
    12. QMessageBox::information(this, tr("Vash Text Editor"), tr("Expression found."));
    13. }
    14. else
    15. {
    16. QMessageBox::information(this, tr("Vash Text Editor"), tr("Expression not found."));
    17. }
    18. }
    To copy to clipboard, switch view to plain text mode 
    Seems fine, but to debug what does following code produce (at the end of your function)?

    Qt Code:
    1. qWarning() << searchString;
    2. qWarning() << documentEdit->toPlainText();
    3. qWarning() << documentEdit->textCursor()->position();
    To copy to clipboard, switch view to plain text mode 



    Qt Code:
    1. void FindDialog::toggleFindButton()
    2. {
    3. QString currentText = findLineEdit->text();
    4.  
    5. if (!currentText.isEmpty())
    6. {
    7. findButton->setEnabled(true);
    8. }
    9. else
    10. {
    11. findButton->setEnabled(false);
    12. }
    13. }
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. findButton->setEnabled(!findLineEdit->text().isEmpty());
    To copy to clipboard, switch view to plain text mode 
    does the same.

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

  5. #5
    Join Date
    Sep 2006
    Posts
    13
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Problem finding text in a QTextEdit

    Quote Originally Posted by Lykurg View Post
    Hi,


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

    Qt Code:
    1. qWarning() << searchString;
    2. qWarning() << documentEdit->toPlainText();
    3. qWarning() << documentEdit->textCursor()->position();
    To copy to clipboard, switch view to plain text mode 
    After adding the qWarning calls, this is what i get for output.

    Qt Code:
    1. $ ./VashTextEditor
    2. "test" <-- qWarning() << searchString
    3. "test" <-- qWarning() << documentEdit->toPlainText()
    To copy to clipboard, switch view to plain text mode 

    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.

  6. #6
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Problem finding text in a QTextEdit

    and what is
    Qt Code:
    1. qWarning() << documentEdit->textCursor()->position();
    To copy to clipboard, switch view to plain text mode 
    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.

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

    Vash5556 (18th May 2009)

  8. #7
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Problem finding text in a QTextEdit

    if it is still not working you may want use the advanced option of QTextDocument. (-> QTextEdit::document())

  9. #8
    Join Date
    Nov 2007
    Posts
    31
    Thanks
    2
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Post Re: Problem finding text in a QTextEdit

    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.
    Attached Files Attached Files
    kichi

  10. #9
    Join Date
    Sep 2006
    Posts
    13
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Problem finding text in a QTextEdit

    Quote Originally Posted by Lykurg View Post
    and what is
    Qt Code:
    1. qWarning() << documentEdit->textCursor()->position();
    To copy to clipboard, switch view to plain text mode 
    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.

Similar Threads

  1. Doubt about QTextEdit and Rich Text
    By iamjayanth in forum Qt Programming
    Replies: 1
    Last Post: 8th April 2009, 08:11
  2. Text Shadowed Problem
    By QbelcorT in forum Qt Programming
    Replies: 0
    Last Post: 23rd March 2009, 01:12
  3. QTextEdit API questions (plain text)
    By Gaspar in forum Qt Programming
    Replies: 4
    Last Post: 16th May 2006, 06:03
  4. Replies: 7
    Last Post: 15th February 2006, 11:34
  5. Problem with inserting text into QTextEdit
    By xorrr in forum Qt Programming
    Replies: 0
    Last Post: 6th February 2006, 11:45

Tags for this Thread

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.