<?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[how to uppercase first letter of sentense in QTextEdit]]></title><description><![CDATA[<p dir="auto">i would try to write a notepad and i try automatically upper first letter in each sentence. Does anyone know how to do it?</p>
]]></description><link>https://forum.qt.io/topic/105303/how-to-uppercase-first-letter-of-sentense-in-qtextedit</link><generator>RSS for Node</generator><lastBuildDate>Mon, 06 Apr 2026 09:35:17 GMT</lastBuildDate><atom:link href="https://forum.qt.io/topic/105303.rss" rel="self" type="application/rss+xml"/><pubDate>Wed, 24 Jul 2019 13:59:15 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to how to uppercase first letter of sentense in QTextEdit on Fri, 26 Jul 2019 08:26:47 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/jonb">@<bdi>jonb</bdi></a> said in <a href="/post/543036">how to uppercase first letter of sentense in QTextEdit</a>:</p>
<blockquote>
<p dir="auto">Maybe, but you will get fined for crossing the road when the light is not right for pedestrians</p>
</blockquote>
<p dir="auto">That's true for almost everywhere except the UK, not only Germany.</p>
]]></description><link>https://forum.qt.io/post/543038</link><guid isPermaLink="true">https://forum.qt.io/post/543038</guid><dc:creator><![CDATA[sierdzio]]></dc:creator><pubDate>Fri, 26 Jul 2019 08:26:47 GMT</pubDate></item><item><title><![CDATA[Reply to how to uppercase first letter of sentense in QTextEdit on Fri, 26 Jul 2019 09:29:50 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/pl45m4">@<bdi>pl45m4</bdi></a><br />
We are English.  Along with soccer, rugby &amp; cricket, we invented spaces, and capital letters.  So we can do with them as we like ;-)</p>
<blockquote>
<p dir="auto">You wont get fined in Germany for not using spaces after a period in your text document :)</p>
</blockquote>
<p dir="auto">Maybe, but you will get fined for crossing the road when the light is not right for pedestrians, or hanging your washing outside.</p>
<p dir="auto">As I said, you are doubtless correct and the OP does want a key stroke approach.</p>
]]></description><link>https://forum.qt.io/post/543036</link><guid isPermaLink="true">https://forum.qt.io/post/543036</guid><dc:creator><![CDATA[JonB]]></dc:creator><pubDate>Fri, 26 Jul 2019 09:29:50 GMT</pubDate></item><item><title><![CDATA[Reply to how to uppercase first letter of sentense in QTextEdit on Fri, 26 Jul 2019 08:03:21 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/jonb">@<bdi>JonB</bdi></a> said in <a href="/post/543024">how to uppercase first letter of sentense in QTextEdit</a>:</p>
<blockquote>
<p dir="auto">If we want to type 2 spaces, or no spaces, or anything else, it's our keyboard, we don't expect the Government to dictate!  This is why we voted to leave the EU, so that we can type spaces as we please ;-)</p>
</blockquote>
<p dir="auto">I think, not using spaces after a period is concidered wrong everywhere (or at least not very professional) :) Even if you drive on the left side of the road :-)<br />
You wont get fined in Germany for not using spaces after a period in your text document :)</p>
<p dir="auto">Most (simple) programs I know, will capitalise the next letter after typing a dot AND a space. Can be very annoying, if you dont want a capital letter.<br />
If you move the cursor or delete some letters in between, this doesnt work anymore (but these simple programs dont care :) ).</p>
<p dir="auto">Unless the OP wants to go for a text correction software (like MS Word) it should be fine.</p>
]]></description><link>https://forum.qt.io/post/543033</link><guid isPermaLink="true">https://forum.qt.io/post/543033</guid><dc:creator><![CDATA[Pl45m4]]></dc:creator><pubDate>Fri, 26 Jul 2019 08:03:21 GMT</pubDate></item><item><title><![CDATA[Reply to how to uppercase first letter of sentense in QTextEdit on Fri, 26 Jul 2019 07:22:01 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/aha_1980">@<bdi>aha_1980</bdi></a><br />
Hi aha, you &amp; I have always gotten on fine, I was a bit shocked by the tone of your admonition!  I had not realised you are German.  In England things are a bit different.  If we want to type 2 spaces, or no spaces, or anything else, it's our keyboard, we don't expect the Government to dictate!  This is why we voted to leave the EU, so that we can type spaces as we please ;-)</p>
<p dir="auto">You will note I did not say the way I mentioned is "better".  It's just how I thought of it.  As I said it totally depends on how the OP wants things to behave.  The key press approach is a bit like a "typewriter" to me, I was thinking more of a "word processor" adjusting all text in a blob.</p>
<p dir="auto">I think of things like: at first I type:</p>
<pre><code>Hello world.goodbye world.
</code></pre>
<p dir="auto">I accidentally missed typing the space after the <code>.</code>.  So I go back and put the space in:</p>
<pre><code>Hello world. goodbye world.
</code></pre>
<p dir="auto">You are not going to capitalise the <code>g</code>.  You also have quite a bit more work to do to recognise other sentence-ending punctuations than <code>.</code>.  Also, do all languages work by the capital letter you want is always the key you pressed plus <strong>Shift</strong>?  I don't know, I thought some would have more complex rules than that, alternative capital letters or not necessarily just via <strong>Shift</strong>, etc.</p>
<p dir="auto">The way I thought of it was to write a regular expression (or similar) to do <em>all</em> capitalization over the complete text, as a generic function (which could be used from anywhere).  Then something like:</p>
<pre><code>def textEdit_textChanged(self):  # or textEdit_textEdited, depending on what you want
    newText = capitalize_via_regular_expression(self.textEdit.text())
    if newText != oldText:
        self.textEdit.setText(newText)
</code></pre>
<p dir="auto">I only change the text when it does need changing, which is not very often per keystroke, but I do admit I run the reg ex once per key stroke.  OK if it's small, not so good if you're typing in the complete works of Shakespeare!</p>
<p dir="auto">As I said, I don't claim it's "better", only that it's "different" from the key press approach.  Pros &amp; cons to each.  Perhaps I should never have brought it up!  You are probably right and the OP had the "typewriter" approach in mind for desired behaviour....</p>
<p dir="auto">Freundliche Gruesse</p>
]]></description><link>https://forum.qt.io/post/543024</link><guid isPermaLink="true">https://forum.qt.io/post/543024</guid><dc:creator><![CDATA[JonB]]></dc:creator><pubDate>Fri, 26 Jul 2019 07:22:01 GMT</pubDate></item><item><title><![CDATA[Reply to how to uppercase first letter of sentense in QTextEdit on Fri, 26 Jul 2019 06:18:38 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/jonb">@<bdi>jonb</bdi></a> said in <a href="/post/542963">how to uppercase first letter of sentense in QTextEdit</a>:</p>
<blockquote>
<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/aha_1980">@<bdi>aha_1980</bdi></a></p>
<blockquote>
<p dir="auto">Which is wrong.</p>
</blockquote>
<p dir="auto">I beg your pardon!?</p>
</blockquote>
<p dir="auto">Ok, sorry. Maybe I was too harsh. You can write as many spaces as you like.</p>
<p dir="auto">Should also be no big deal to handle this in code.</p>
<blockquote>
<blockquote>
<p dir="auto">There is a DIN norm for machine typing which regulates that.</p>
</blockquote>
<p dir="auto">Wot?</p>
</blockquote>
<p dir="auto">Germany has standards for <em>everything</em>.</p>
<p dir="auto">Still, you claim you can do better without <code>keyPressEvent</code>. I'm waiting for the proof :)</p>
<p dir="auto">Regards</p>
]]></description><link>https://forum.qt.io/post/543010</link><guid isPermaLink="true">https://forum.qt.io/post/543010</guid><dc:creator><![CDATA[aha_1980]]></dc:creator><pubDate>Fri, 26 Jul 2019 06:18:38 GMT</pubDate></item><item><title><![CDATA[Reply to how to uppercase first letter of sentense in QTextEdit on Thu, 25 Jul 2019 20:00:11 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/aha_1980">@<bdi>aha_1980</bdi></a></p>
<blockquote>
<p dir="auto">Which is wrong.</p>
</blockquote>
<p dir="auto">I beg your pardon!?</p>
<blockquote>
<p dir="auto">There is a DIN norm for machine typing which regulates that.</p>
</blockquote>
<p dir="auto">Wot?</p>
]]></description><link>https://forum.qt.io/post/542963</link><guid isPermaLink="true">https://forum.qt.io/post/542963</guid><dc:creator><![CDATA[JonB]]></dc:creator><pubDate>Thu, 25 Jul 2019 20:00:11 GMT</pubDate></item><item><title><![CDATA[Reply to how to uppercase first letter of sentense in QTextEdit on Thu, 25 Jul 2019 19:53:14 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/jonb">@<bdi>JonB</bdi></a></p>
<blockquote>
<p dir="auto">personally I always type two spaces after a period and</p>
</blockquote>
<p dir="auto">Which is wrong. There is a DIN norm for machine typing which regulates that.</p>
<p dir="auto">Anyway, such cases can be covered with slightly more code.</p>
<p dir="auto">But how would you do that with <code>textChanged</code> in a simpler way?</p>
]]></description><link>https://forum.qt.io/post/542962</link><guid isPermaLink="true">https://forum.qt.io/post/542962</guid><dc:creator><![CDATA[aha_1980]]></dc:creator><pubDate>Thu, 25 Jul 2019 19:53:14 GMT</pubDate></item><item><title><![CDATA[Reply to how to uppercase first letter of sentense in QTextEdit on Thu, 25 Jul 2019 19:37:22 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/aha_1980">@<bdi>aha_1980</bdi></a><br />
Yes, that is fine.  But most of your code is spent looking at the document state instead of prior key presses, which is my point.  Originally you talked about "remember the last two keys that were pressed", you're not doing that now.</p>
<p dir="auto">Obviously the key press way of doing things doesn't go anywhere toward capitalising sentences in an arbitrary piece of existing text put into a <code>QTextEdit</code>.  It depends what the OP wants his editor to do.</p>
<p dir="auto">In your code, if the first char I type in is a digit from the keys which have a punctuation character on the <strong>Shift</strong> of the key, what happens?  This whole business of shift-modifier-on-a-key-press in order to guarantee an upper-case letter seems dubious to me....!  Oh, and BTW: <em>personally</em> I <em>always</em> type <strong>two</strong> spaces after a period and before the first character of the next sentence, and I'm not totally alone.  You'll have to start changing your code to accommodate me!  Which is why my first inclination was to do it all via text changed and regular expressions.  But probably not what the OP has in mind :)</p>
]]></description><link>https://forum.qt.io/post/542941</link><guid isPermaLink="true">https://forum.qt.io/post/542941</guid><dc:creator><![CDATA[JonB]]></dc:creator><pubDate>Thu, 25 Jul 2019 19:37:22 GMT</pubDate></item><item><title><![CDATA[Reply to how to uppercase first letter of sentense in QTextEdit on Thu, 25 Jul 2019 19:21:28 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/jonb">@<bdi>JonB</bdi></a> I still think handling this in the <code>keyPressEvent</code> handler is better, as you manipulate the char before it is inserted. And it's not much code, really:</p>
<pre><code>void TextEditor::keyPressEvent(QKeyEvent *e)
{
    QChar lastChar = document()-&gt;characterAt(textCursor().position() - 1);
    QChar secondLastChar = document()-&gt;characterAt(textCursor().position() - 2);

    // If we are at the beginning of text, start a new line,
    // or start a new sentence, include an uppercase letter
    if (textCursor().atStart()
            || lastChar == QChar::ParagraphSeparator
            || lastChar == QChar::LineSeparator
            || (lastChar == QChar::Space &amp;&amp; secondLastChar == '.')) {
        QKeyEvent event(e-&gt;type(), e-&gt;key(), e-&gt;modifiers() | Qt::ShiftModifier,
                        e-&gt;text().toUpper(), e-&gt;isAutoRepeat(), ushort(e-&gt;count()));

        QPlainTextEdit::keyPressEvent(&amp;event);
        return;
    }

    QPlainTextEdit::keyPressEvent(e);
}
</code></pre>
]]></description><link>https://forum.qt.io/post/542934</link><guid isPermaLink="true">https://forum.qt.io/post/542934</guid><dc:creator><![CDATA[aha_1980]]></dc:creator><pubDate>Thu, 25 Jul 2019 19:21:28 GMT</pubDate></item><item><title><![CDATA[Reply to how to uppercase first letter of sentense in QTextEdit on Thu, 25 Jul 2019 19:14:16 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/stan09">@<bdi>stan09</bdi></a> I've added an example to my <a href="https://github.com/andre-hartmann/QtExamples/tree/master/TextEditor" target="_blank" rel="noopener noreferrer nofollow ugc">github</a>.</p>
]]></description><link>https://forum.qt.io/post/542931</link><guid isPermaLink="true">https://forum.qt.io/post/542931</guid><dc:creator><![CDATA[aha_1980]]></dc:creator><pubDate>Thu, 25 Jul 2019 19:14:16 GMT</pubDate></item><item><title><![CDATA[Reply to how to uppercase first letter of sentense in QTextEdit on Thu, 25 Jul 2019 17:20:10 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/stan09">@<bdi>stan09</bdi></a></p>
<blockquote>
<p dir="auto">Yes, it works, but only with first letter in whole document. How add condition to repeat it after each dot?</p>
</blockquote>
<p dir="auto">That's where you have to save state somehow to look at what has gone before.  You have to figure the logic for what you want to achieve.</p>
<p dir="auto">Given that you're doing it via key press events.  I wouldn't do <a class="plugin-mentions-user plugin-mentions-a" href="/user/aha_1980">@<bdi>aha_1980</bdi></a>'s (self-acknowledged) hacky "just remember last two keys", you'll want to take account of things like if the user hits <strong>Backspace</strong> I would have thought at minimum, it just gets complicated.</p>
<p dir="auto">His using <code>QTextCursor</code> to look at what's in the line would be one way of getting the state you need.</p>
<p dir="auto">I will just say: you <em>could</em> do it all via <code>textChanged</code> slot &amp; <code>setText</code> when needed (which won't be often, once per sentence), instead of the key press route.  It will cater for lots of cases key presses will not, it may be less code.  Depends on how you want your "notepad" to work.</p>
]]></description><link>https://forum.qt.io/post/542919</link><guid isPermaLink="true">https://forum.qt.io/post/542919</guid><dc:creator><![CDATA[JonB]]></dc:creator><pubDate>Thu, 25 Jul 2019 17:20:10 GMT</pubDate></item><item><title><![CDATA[Reply to how to uppercase first letter of sentense in QTextEdit on Thu, 25 Jul 2019 15:40:56 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/stan09">@<bdi>stan09</bdi></a></p>
<p dir="auto">The proper way is to use <a href="https://doc.qt.io/qt-5/richtext-cursor.html" target="_blank" rel="noopener noreferrer nofollow ugc">QTextCursor</a> to scan backwards from your actual position and see if you have a dot '.' followed by a space ' '.</p>
<p dir="auto">A hacky solution would be to remember the last two keys that were pressed in <code>keyPressEvent</code>. That would, however, not work correctly if you move the cursor while writing.</p>
<p dir="auto">Regards</p>
]]></description><link>https://forum.qt.io/post/542913</link><guid isPermaLink="true">https://forum.qt.io/post/542913</guid><dc:creator><![CDATA[aha_1980]]></dc:creator><pubDate>Thu, 25 Jul 2019 15:40:56 GMT</pubDate></item><item><title><![CDATA[Reply to how to uppercase first letter of sentense in QTextEdit on Thu, 25 Jul 2019 14:02:38 GMT]]></title><description><![CDATA[<p dir="auto">At least works fine.<br />
<a class="plugin-mentions-user plugin-mentions-a" href="/user/j.hilk">@<bdi>J.Hilk</bdi></a></p>
<blockquote>
<p dir="auto">btw, calling each key stroke toPlainText() maybe costly and/or overkill, it's just the first thing that came to my mind. You should investigate if that can't be done better.</p>
</blockquote>
<p dir="auto">How to call out funcion only after pressing a space?</p>
]]></description><link>https://forum.qt.io/post/542899</link><guid isPermaLink="true">https://forum.qt.io/post/542899</guid><dc:creator><![CDATA[stan09]]></dc:creator><pubDate>Thu, 25 Jul 2019 14:02:38 GMT</pubDate></item><item><title><![CDATA[Reply to how to uppercase first letter of sentense in QTextEdit on Thu, 25 Jul 2019 11:56:22 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/stan09">@<bdi>stan09</bdi></a></p>
<blockquote>
<p dir="auto">How add condition to repeat it after each dot?</p>
</blockquote>
<pre><code>QString plaintText = toPlainText();
if(plaintText == QString() || plaintText.endsWith('.'){
</code></pre>
<p dir="auto">btw, calling each key stroke toPlainText() maybe costly and/or overkill, it's just the first thing that came to my mind. You should investigate if that can't be done better.</p>
<blockquote>
<p dir="auto">Btw, how can i add this code to projekt with base class QMainWindow?</p>
</blockquote>
<p dir="auto">by creating a new instance ? Or do you mean inside the QDesigner ?<br />
In later case take a look here:<br />
<a href="https://doc.qt.io/qt-5/designer-using-custom-widgets.html" target="_blank" rel="noopener noreferrer nofollow ugc">https://doc.qt.io/qt-5/designer-using-custom-widgets.html</a></p>
]]></description><link>https://forum.qt.io/post/542857</link><guid isPermaLink="true">https://forum.qt.io/post/542857</guid><dc:creator><![CDATA[J.Hilk]]></dc:creator><pubDate>Thu, 25 Jul 2019 11:56:22 GMT</pubDate></item><item><title><![CDATA[Reply to how to uppercase first letter of sentense in QTextEdit on Thu, 25 Jul 2019 11:49:47 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/j.hilk">@<bdi>J.Hilk</bdi></a><br />
Yes, it works, but only with first letter in whole document. How add condition to repeat it after each dot?<br />
Btw, how can i add this code to projekt with base class QMainWindow? I can run your code only in separate projekt.</p>
]]></description><link>https://forum.qt.io/post/542853</link><guid isPermaLink="true">https://forum.qt.io/post/542853</guid><dc:creator><![CDATA[stan09]]></dc:creator><pubDate>Thu, 25 Jul 2019 11:49:47 GMT</pubDate></item><item><title><![CDATA[Reply to how to uppercase first letter of sentense in QTextEdit on Thu, 25 Jul 2019 09:56:29 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/stan09">@<bdi>stan09</bdi></a></p>
<pre><code>#ifndef MYTEXTEDIT_H
#define MYTEXTEDIT_H

#include &lt;QTextEdit&gt;
#include &lt;QKeyEvent&gt;

class myTextEdit : public QTextEdit
{
    Q_OBJECT
public:
    explicit myTextEdit(QWidget *parent = nullptr) : QTextEdit (parent)
    {
        
    }

protected:
    virtual void keyPressEvent(QKeyEvent *e)override
    {
        if(toPlainText() == QString()){
            if((e-&gt;modifiers() &amp; Qt::ShiftModifier)  == 0){
                e-&gt;setModifiers((e-&gt;modifiers() | Qt::ShiftModifier));
                QKeyEvent *event = new QKeyEvent(e-&gt;type(),e-&gt;key(),e-&gt;modifiers(),e-&gt;text().toUpper(), e-&gt;isAutoRepeat(),e-&gt;count());
                QTextEdit::keyPressEvent(event);
            }
        } else {
            QTextEdit::keyPressEvent(e);
        }
    }
};

#endif // MYTEXTEDIT_H
</code></pre>
]]></description><link>https://forum.qt.io/post/542821</link><guid isPermaLink="true">https://forum.qt.io/post/542821</guid><dc:creator><![CDATA[J.Hilk]]></dc:creator><pubDate>Thu, 25 Jul 2019 09:56:29 GMT</pubDate></item><item><title><![CDATA[Reply to how to uppercase first letter of sentense in QTextEdit on Thu, 25 Jul 2019 09:23:49 GMT]]></title><description><![CDATA[<p dir="auto">Thanks Andy<br />
I've tried to use keyPressEvent(), but it comsumed each letter i've typed. I don't have experience with QtextBoundaryFinder(), it's one of my first's apps. Could you explain how to implement your method?</p>
]]></description><link>https://forum.qt.io/post/542817</link><guid isPermaLink="true">https://forum.qt.io/post/542817</guid><dc:creator><![CDATA[stan09]]></dc:creator><pubDate>Thu, 25 Jul 2019 09:23:49 GMT</pubDate></item><item><title><![CDATA[Reply to how to uppercase first letter of sentense in QTextEdit on Thu, 25 Jul 2019 07:05:26 GMT]]></title><description><![CDATA[<p dir="auto">There is no direct way of doing it at least, but you could override the keyPressEvent() for QTextEdit and if it is going to be a normal key then with the help of <a href="https://doc.qt.io/qt-5/qtextboundaryfinder.html" target="_blank" rel="noopener noreferrer nofollow ugc">QTextBoundaryFinder</a> you can automatically make it upper case by inserting the character directly instead of letting the base implementation handle it for you.</p>
]]></description><link>https://forum.qt.io/post/542746</link><guid isPermaLink="true">https://forum.qt.io/post/542746</guid><dc:creator><![CDATA[AndyS]]></dc:creator><pubDate>Thu, 25 Jul 2019 07:05:26 GMT</pubDate></item></channel></rss>