<div dir="ltr"><pre style="background-color:rgb(255,255,255);color:rgb(0,0,0);font-family:"DejaVu Sans Mono";font-size:9.1pt">Hi,<br>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 <br>I need to know how to count my search results.for example if there are 6 highlighted item I want to count them.<br><br>    m = <span style="color:rgb(0,0,255)">1<br></span><span style="color:rgb(0,0,255)">    </span>cur = conn.cursor()<br>    conn.text_factory = <span style="color:rgb(0,0,128)">str<br></span><span style="color:rgb(0,0,128)">    </span>cur.execute(<span style="color:rgb(0,128,128);font-weight:bold">" SELECT text FROM Translation WHERE data = ?"</span>, (m,))<br>    <span style="color:rgb(148,85,141)">self</span>.SearchResults.clear()<br>    a = <span style="color:rgb(0,0,255)">1<br></span><span style="color:rgb(0,0,255)">    </span><span style="color:rgb(0,0,128);font-weight:bold">for </span>row <span style="color:rgb(0,0,128);font-weight:bold">in </span>cur:<br>        <span style="color:rgb(148,85,141)">self</span>.SearchResults.append(<span style="color:rgb(0,128,128);font-weight:bold">'{0} )- {1}'</span>.format(a , <span style="color:rgb(0,0,128)">str</span>(row[<span style="color:rgb(0,0,255)">0</span>])))<br>        <span style="color:rgb(148,85,141)">self</span>.SearchResults.append(<span style="color:rgb(0,128,128);font-weight:bold">""</span>)<br>        <span style="color:rgb(148,85,141)">#self</span>.text = <span style="color:rgb(0,128,128);font-weight:bold">'{0} )- {1}'</span>.format(a , <span style="color:rgb(0,0,128)">str</span>(row[<span style="color:rgb(0,0,255)">0</span>]))<br>        a = a + <span style="color:rgb(0,0,255)">1<br></span><span style="color:rgb(0,0,255)">    </span><span style="color:rgb(0,0,128);font-weight:bold">return<br></span><span style="color:rgb(0,0,128);font-weight:bold">def </span>Searchfor(<span style="color:rgb(148,85,141)">self</span>):<br><br><br>    cursor = <span style="color:rgb(148,85,141)">self</span>.SearchResults.textCursor(<wbr>)<br>    format = QtGui.QTextCharFormat()<br>    format.setForeground(QtGui.<wbr>QBrush(QtGui.QColor(<span style="color:rgb(0,128,128);font-weight:bold">"orange"</span>))<wbr>)<br>    pattern = <span style="color:rgb(148,85,141)">self</span>.lineEdit.text()<br>    regex = QtCore.QRegExp(pattern)<br><br>    pos = <span style="color:rgb(0,0,255)">0<br></span><span style="color:rgb(0,0,255)">    </span>index = regex.indexIn(<span style="color:rgb(148,85,141)">self</span>.<wbr>SearchResults.toPlainText(), pos)<br><br>    <span style="color:rgb(0,0,128);font-weight:bold">while </span>(index != -<span style="color:rgb(0,0,255)">1</span>):<br>        <span style="color:rgb(128,128,128);font-style:italic"># Select the matched text and apply the desired format<br></span><span style="color:rgb(128,128,128);font-style:italic">        </span>cursor.setPosition(index)<br>        cursor.movePosition(QtGui.<wbr>QTextCursor.EndOfWord, <span style="color:rgb(0,0,255)">1</span>)<br>        cursor.mergeCharFormat(format)<br>        <span style="color:rgb(128,128,128);font-style:italic"># Move to the next match<br></span><span style="color:rgb(128,128,128);font-style:italic">        </span>pos = index + regex.matchedLength()<br>        index = regex.indexIn(<span style="color:rgb(148,85,141)">self</span>.<wbr>SearchResults.toPlainText(), pos)</pre></div>