<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Problem in Check the highlighted string in QPlainTextEdit]]></title><description><![CDATA[<p dir="auto">I have a QPlainTextEdit which have a highlighted piece of string in it using this code to make the highlight</p>
<pre><code>    QString str = "an";
    ui-&gt;plainTextEdit-&gt;setPlainText("Hello world this\nan example to test");
    int begin = ui-&gt;plainTextEdit-&gt;toPlainText().indexOf(str); // get index of word start
    int end = begin + str.size(); // end of word
    qDebug() &lt;&lt; begin &lt;&lt; " " &lt;&lt; end &lt;&lt; endl;
    QTextCharFormat fmt;
    fmt.setBackground(Qt::yellow); // a char format 
//move on it and start highlight this word
    QTextCursor cursor(ui-&gt;plainTextEdit-&gt;document());
    cursor.setPosition(begin,QTextCursor::MoveAnchor);
    cursor.setPosition(end,QTextCursor::KeepAnchor);
    cursor.setCharFormat(fmt);
</code></pre>
<p dir="auto">and this code to check the highlighted word if it's highlighted or not</p>
<pre><code>    QString str = "an";
    int begin = ui-&gt;plainTextEdit-&gt;toPlainText().indexOf(str);
    int end = begin + str.size();

    QTextCursor cursor(ui-&gt;plainTextEdit-&gt;document());
    cursor.setPosition(begin,QTextCursor::MoveAnchor);
    cursor.setPosition(end,QTextCursor::KeepAnchor);
    QTextCharFormat fmt = cursor.charFormat();
    if (fmt.background() == Qt::yellow) {
        qDebug() &lt;&lt; "YES highlighted";
    }
    else {
        qDebug() &lt;&lt; "NO Not highlighted";
    }
</code></pre>
<p dir="auto">it works fine<br />
The problem is when I begin from early start if i make begin - 10 for example it ouput "yes highlighted" which is wrong so if I make begin = 0 and end stop at the position of the highlighted word it say YES if i increase the end it say no so it work from one side only<br />
but it work when I extend the end if i make for example end + 10 it say no<br />
why it make that and how to fix it<br />
this the example of begin which have the problem</p>
<pre><code>    QString str = "an";
    int begin = 0; // start from 0 which all not highlighted
    int end = ui-&gt;plainTextEdit-&gt;toPlainText().indexOf(str); + str.size(); and stop and end of the highlighted word

    QTextCursor cursor(ui-&gt;plainTextEdit-&gt;document());
    cursor.setPosition(begin,QTextCursor::MoveAnchor);
    cursor.setPosition(end,QTextCursor::KeepAnchor);
    QTextCharFormat fmt = cursor.charFormat();
    if (fmt.background() == Qt::yellow) {
        qDebug() &lt;&lt; "YES"; // it ouput yes because just from the start to end contains a highlighted word
    }
    else {
        qDebug() &lt;&lt; "NO";
    }
</code></pre>
<p dir="auto">Thanks in advance</p>
]]></description><link>https://forum.qt.io/topic/76980/problem-in-check-the-highlighted-string-in-qplaintextedit</link><generator>RSS for Node</generator><lastBuildDate>Thu, 07 May 2026 22:02:18 GMT</lastBuildDate><atom:link href="https://forum.qt.io/topic/76980.rss" rel="self" type="application/rss+xml"/><pubDate>Wed, 08 Mar 2017 10:53:55 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Problem in Check the highlighted string in QPlainTextEdit on Wed, 08 Mar 2017 23:04:02 GMT]]></title><description><![CDATA[<p dir="auto">Hi,</p>
<p dir="auto">Out of curiosity, any reasons for not using a <a href="http://doc.qt.io/qt-5/qsyntaxhighlighter.html" target="_blank" rel="noopener noreferrer nofollow ugc">QSyntaxHighlighte</a>r ?</p>
]]></description><link>https://forum.qt.io/post/380736</link><guid isPermaLink="true">https://forum.qt.io/post/380736</guid><dc:creator><![CDATA[SGaist]]></dc:creator><pubDate>Wed, 08 Mar 2017 23:04:02 GMT</pubDate></item></channel></rss>