Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QTextEdit issue with implementing Search functionality
Qt 6.11 is out! See what's new in the release blog

QTextEdit issue with implementing Search functionality

Scheduled Pinned Locked Moved General and Desktop
4 Posts 2 Posters 2.5k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • V Offline
    V Offline
    vezprog
    wrote on last edited by
    #1

    My main form contains a QTextEdit, 2 File selection QPushButtons, and a QComboBox.

    When the user clicks the Open File push button, the application gets the selected file using a QFileDialog box, and parses the file for keywords as it parses line by line, and then finally prints out the complete file into the QTextEdit.

    The found keywords are then put into the QComboBox to allow the user to jump to the specific locations in the QTextEdit text. To do this, I use a QTextCursor and a QComboBox currentIndexChanged(QString) signal.

    This is the code for the slot connected to the currentIndexChanged(QString)

    @
    void MainWindow::jumpToIndexChanged(QString name)
    {
    // get find position
    int pos = ui->txtfileviewer->find(name);
    fileViewerCursor.setPosition(pos);
    }
    @

    where fileViewerCursor is the QTextCursor.

    The issue I am having is that the last item in the combo box is considered to be the last "keyword" in the file since I am parsing line by line, and once I have moved to the last items index, if I try to go back to any keyword before the last item, it fails find the location of the item (pos returns as 0).

    My thought was on every signal event, reset the cursor to the start of the file, then search so I am always searching from the beginning...but this did not work.

    @
    // set position
    fileViewerCursor.setPosition(0);

    // get find position
    int pos = ui->txtfileviewer->find(name);
    fileViewerCursor.setPosition(pos);
    

    @

    Do anyone have any insight on how I could fix this, or better yet, a more efficient way to do this?

    Thanks in advance.

    1 Reply Last reply
    0
    • ? Offline
      ? Offline
      A Former User
      wrote on last edited by
      #2

      The cursor is a copy:

      bq. from "QTextEdit::textCursor() :"/doc/qt-4.8/qtextedit.html#textCursor
      Returns a copy of the QTextCursor that represents the currently visible cursor. Note that changes on the returned cursor do not affect QTextEdit's cursor; use setTextCursor() to update the visible cursor.

      After you have modified it, you need to set the cursor in the textedit:

      @
      ui->txtfileviewer->setTextCursor(fileViewerCursor);
      @

      1 Reply Last reply
      0
      • V Offline
        V Offline
        vezprog
        wrote on last edited by
        #3

        I am sorry I didnt show this code before.

        My QTextCursor is set as a public variable in my class header. I have this in my initilization...

        @
        // setup the text cursor
        ui->txtfileviewer->setTextCursor(fileViewerCursor);
        fileViewerCursor.movePosition(QTextCursor::Start);
        @

        I guess I am just baffled when I use ui->txtfileviewer->find()
        After I have found something at the end of the file, I am unable to find anything before the end of the document (or text in the txt viewer).

        At this point I dont think it has anything to do with the file cursor...but more with the find index of the QTextEdit.

        1 Reply Last reply
        0
        • ? Offline
          ? Offline
          A Former User
          wrote on last edited by
          #4

          setTextCursor copies the cursor. If you modify it afterwards you need to set it again.

          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved