QTextEdit issue with implementing Search functionality



  • 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.



  • 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);
    @



  • 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.



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


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.