Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7011621
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T22:09:03+00:00 2026-05-27T22:09:03+00:00

The code below does not highlight the search term when it is found. In

  • 0

The code below does not highlight the search term when it is found. In fact the cursor disappears from the QPlainTextEdit (called ui->Editor) after pressing the ‘next’ button. What’s causing it?

void TextEditor::findNextInstanceOfSearchTerm()
{
    QString searchTerm = this->edtFind->text();

    if(this->TextDocument == NULL)
    {
        this->TextDocument = ui->Editor->document();
    }


    QTextCursor documentCursor(this->TextDocument);

    documentCursor = this->TextDocument->find(searchTerm,documentCursor);

    if(!documentCursor.isNull())
    {
        documentCursor.select(QTextCursor::WordUnderCursor);

    }else
    {
        ui->statusbar->showMessage("\""+searchTerm+"\" could not be found",MESSAGE_DURATION);

    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T22:09:04+00:00Added an answer on May 27, 2026 at 10:09 pm

    Firstly, your code creates a new cursor at the beginning of the document each time you press the next button, so you will always search from the beginning. Secondly, you must understand that the cursor you manipulate has nothing to do with the one in your QPlainTextEdit: you manipulate a copy. If you want to impact the text edit, you must modify its cursor using setTextCursor. Here is a working solution:

    void TextEditor::findNextInstanceOfSearchTerm()
    {
        QString searchTerm = this->edtFind->text();
    
        if(this->TextDocument == NULL)
        {
            this->TextDocument = ui->Editor->document();
        }
    
        // get the current cursor
        QTextCursor documentCursor = ui->Editor->textCursor();
    
        documentCursor = this->TextDocument->find(searchTerm,documentCursor);
    
        if(!documentCursor.isNull())
        {
            // needed only if you want the entire word to be selected
            documentCursor.select(QTextCursor::WordUnderCursor);
    
            // modify the text edit cursor
            ui->Editor->setTextCursor(documentCursor);
        }
        else
        {
            ui->statusbar->showMessage(
                "\""+searchTerm+"\" could not be found",MESSAGE_DURATION);
        }
    }
    

    As a side note, you might want to know that QPlainTextEdit provides a find method, so this might be an easier way to achieve what you want:

    void TextEditor::findNextInstanceOfSearchTerm()
    {
        QString searchTerm = this->edtFind->text();
    
        bool found = ui->Editor->find(searchTerm);
    
        if (found)
        {
            QTextCursor cursor = ui->Editor->textCursor();
            cursor.select(QTextCursor::WordUnderCursor);
            ui->Editor->setTextCursor(cursor);
        }
        else
        {
            // set message in status bar
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Not sure how to do this. The code below does not work. From the
Code below does not run correctly and throws InvalidOperationExcepiton . public void Foo() {
In the below code sample, what does {0:X2} mean? This is from the reflection
I am trying to find out why the code below does not seem to
I'm using MSVC and it seems like the code below does not crash and
Any idea why the piece of code below does not add the script element
I would like to use a spinner. But, this code below does not display
The code below does not compile. The compiler error is Cannot implicitly convert type
The code below does not adding rectangle. Can anyone advice What i missed ?
The code below does NOT work: Cause: I assume I tracked down the cause

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.