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. QTextDocument find returns wrong cursor position
Qt 6.11 is out! See what's new in the release blog

QTextDocument find returns wrong cursor position

Scheduled Pinned Locked Moved General and Desktop
1 Posts 1 Posters 805 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.
  • Q Offline
    Q Offline
    quserop
    wrote on last edited by
    #1

    I am trying to make a search dialog for my application, right now I am working on the find function. So far it works, but it only finds the first match in the text area and it won't search past that:

    @
    Find: a
    b
    b
    a <--- When ever I hit 'find' it goes here no matter where the text cursor is
    b
    b
    a <--- it won't go here unless I get rid of the first 'a'@

    Here is my code:

    @
    #!/usr/bin/python

    import sys
    from PyQt4 import QtGui, QtCore

    class Example(QtGui.QWidget):

    def __init__(self):
        super(Example, self).__init__()
        self.initUI()
        
    def initUI(self):      
    
        button = QtGui.QPushButton('Find', self)
        
        self.lineEdit = QtGui.QLineEdit(self)
        self.textEdit = QtGui.QTextEdit(self)
        
        button.move(1, 200)
        self.lineEdit.move(100, 203)
        
        button.clicked.connect(self.TextSearch)
        
        self.setGeometry(300, 300, 260, 230)
        self.setWindowTitle('QtGui.QLineEdit')
        self.show()
        
    def TextSearch(self):
    
      searchString = self.lineEdit.text();  # text to look for
      document = self.textEdit.document(); # File
     
      if searchString.isEmpty():
        print "Nothing to search for!"
        return
     
      c = QtGui.QTextCursor(document);
     
      while not c.isNull() and not c.atEnd():
        c = document.find(searchString, c, QtGui.QTextDocument.FindWholeWords);
     
        if not c.isNull():
          self.textEdit.setTextCursor(c);
          self.textEdit.setFocus()
          return
     
      print "Word not found."
    

    def main():

    app = QtGui.QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())
    

    if name == 'main':
    main()@

    Look at TextSearch for the part where I search for the text.

    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