<?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[&#x27;insert&#x27; method of a QLineEdit object doesn&#x27;t delete selected text]]></title><description><![CDATA[<p dir="auto">Hello, I'm looking for help to solve a problem with QLineEdit.</p>
<p dir="auto">When I press a special button the following char sequence is set on the QLineEdit object:</p>
<p dir="auto">add($,$)<br />
Each $ character is a placeholder for an int, which I'm supposed to provide by clicking on the [0-9] buttons. Each click inserts a new digit until I press Enter. Then the cursor moves to the second placeholder, selects it and wait for my click. But there is a problem.</p>
<p dir="auto">When add($,$) appears, I select the first placeholder and the program wait for the first digit which further substitutes the placeholder by itself. But when I finish entering the first number and move on to entering the second number each new digit doesn't erase the $ char, it just shifts it right. I tried almost everything I could imagine and it still doesn't work.</p>
<p dir="auto">Can anyone help with an advice please? Here is the code:</p>
<pre><code>void MainWindow::operation_clicked()
{
    QString operation((qobject_cast&lt;QPushButton*&gt;(sender()))-&gt;text());
    if(operation == "add($,$)")
    {
        ui-&gt;lineEdit-&gt;insert(operation);
        ui-&gt;lineEdit-&gt;cursorBackward(false, 3);
        ui-&gt;lineEdit-&gt;setSelection(ui-&gt;lineEdit-&gt;cursorPosition(),-1);
    }
}
```

```
void MainWindow::digit_clicked()
{
    if(expected_operands != 0 &amp;&amp; printed_digits &lt; MAXLEN)
    {
        QString num((qobject_cast&lt;QPushButton*&gt;(sender()))-&gt;text());
        ui-&gt;lineEdit-&gt;insert(num);
        ui-&gt;lineEdit-&gt;setFocus();
        ++printed_digits;
    }
}
``

```
void MainWindow::enter_pressed()
{
    if(expected_operands != 0)
    {
        ui-&gt;lineEdit-&gt;setSelection(ui-&gt;lineEdit-&gt;cursorPosition(), (-printed_digits-sign));
        current_argument = (QString(ui-&gt;lineEdit-&gt;selectedText())).toInt();
        --expected_operands;
        ui-&gt;lineEdit-&gt;deselect();
        if(expected_operands != 0)
        {
            ui-&gt;lineEdit-&gt;cursorForward(false, sign+printed_digits+2);
            ui-&gt;lineEdit-&gt;setSelection(ui-&gt;lineEdit-&gt;cursorPosition(),-1);
        }
        ui-&gt;lineEdit-&gt;setFocus();
        printed_digits = 0;
    }
}

````
Everything is realised using SIGNAL-SLOT mechanism.</code></pre>
]]></description><link>https://forum.qt.io/topic/95076/insert-method-of-a-qlineedit-object-doesn-t-delete-selected-text</link><generator>RSS for Node</generator><lastBuildDate>Mon, 21 Sep 2026 16:15:24 GMT</lastBuildDate><atom:link href="https://forum.qt.io/topic/95076.rss" rel="self" type="application/rss+xml"/><pubDate>Thu, 27 Sep 2018 16:17:24 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to &#x27;insert&#x27; method of a QLineEdit object doesn&#x27;t delete selected text on Thu, 27 Sep 2018 20:02:08 GMT]]></title><description><![CDATA[<p dir="auto">Hi and welcome to devnet,</p>
<p dir="auto">Can you reduce that to a minimal compilable example that shows the behaviour ?</p>
]]></description><link>https://forum.qt.io/post/484040</link><guid isPermaLink="true">https://forum.qt.io/post/484040</guid><dc:creator><![CDATA[SGaist]]></dc:creator><pubDate>Thu, 27 Sep 2018 20:02:08 GMT</pubDate></item></channel></rss>