[PyQt] how to clear last searched items in my code and how to count my searched item.

Maziar Parsijani maziar.parsijani at gmail.com
Mon Aug 6 13:10:16 BST 2018


Hi,
I have this code for searching in a Qtextedit.My first problem is that
I can not clear the search result to start a new one.The other thing
that
I need to know how to count my search results.for example if there are
6 highlighted item I want to count them.

    m = 1
    cur = conn.cursor()
    conn.text_factory = str
    cur.execute(" SELECT text FROM Translation WHERE data = ?", (m,))
    self.SearchResults.clear()
    a = 1
    for row in cur:
        self.SearchResults.append('{0} )- {1}'.format(a , str(row[0])))
        self.SearchResults.append("")
        #self.text = '{0} )- {1}'.format(a , str(row[0]))
        a = a + 1
    return
def Searchfor(self):


    cursor = self.SearchResults.textCursor()
    format = QtGui.QTextCharFormat()
    format.setForeground(QtGui.QBrush(QtGui.QColor("orange")))
    pattern = self.lineEdit.text()
    regex = QtCore.QRegExp(pattern)

    pos = 0
    index = regex.indexIn(self.SearchResults.toPlainText(), pos)

    while (index != -1):
        # Select the matched text and apply the desired format
        cursor.setPosition(index)
        cursor.movePosition(QtGui.QTextCursor.EndOfWord, 1)
        cursor.mergeCharFormat(format)
        # Move to the next match
        pos = index + regex.matchedLength()
        index = regex.indexIn(self.SearchResults.toPlainText(), pos)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://www.riverbankcomputing.com/pipermail/pyqt/attachments/20180806/0e0fd4f6/attachment.html>


More information about the PyQt mailing list