[Qt-creator] R.I.P. for QPlainTextEdit in Qr Creator 17?

Henry Skoglund henry at tungware.se
Thu Jun 26 20:01:41 CEST 2025


On 2025-06-25 10:53, Eike Ziller wrote:
> Am 21.06.25 um 16:17 schrieb Henry Skoglund:
>>>> On 2025-06-19 22:51, apoenitz wrote:
>>>>> Hi.
>>>>>
>>>>> On Wed, Jun 18, 2025 at 06:05:08PM +0200, Henry Skoglund wrote:
>>>>>> Hi, just updated to Qt Creator 17 and it looks good, however when rebuilding
>>>>>> my Qt Creator plugin I got stuck for a bit with this failing:
>>>>>>
>>>>>>     auto pe =
>>>>>> qobject_cast<QPlainTextEdit*>(EditorManager::currentEditor()->widget());
>>>>>>
>>>>>> For almost 15 years that statement served me well and it allowed for example
>>>>>> setting a fatter cursor at Qt Creator start (useful on 4K screens) but
>>>>>> today, it returns a nullptr :-(
>>>>>>
>>>>>>
>>>>>> Instead, the new incantation for Qt Creator 17 (and onwards?) seems to be:
>>>>>>
>>>>>>     auto pe = qobject_cast<Utils::PlainTextEdit*>(EditorManager::currentEditor()- >widget());
>>>>>>
>>>>>> After that fix, it's smooth sailing (Utils::PlainTextEdit behaves 100%
>>>>>> compatible with QPlainTextEdit).
>>>>>>
>>>>>> But if there was a bit of documentation of this new class in the Utils::
>>>>>> Namespace I would be even more happy!
>>>>> Sorry to hear that this bit you.
>>>>>
>>>>> I guess nobody expected that some plugin out there had a hard dependency
>>>>> on Editor::widget() being in fact a QPlainTextEdit for text editors.
>>>>>
>>>>> The fact that Utils::PlainTextEdit behaves like QPlainTextEdit is not
>>>>> quite an accident, the idea is add a few features and try to upstream
>>>>> it to Qt proper again. For this to happen it has to stay compatible.
>>>>>
>>>>> In case this upstreaming will be  successful there's a good chance that
>>>>> Utils::PlainTextEdit will vanish again. But then there'll hopefully
>>>>> be a more communication.
>>>>>
>>>> Thanks for answering, it's all working fine now with the new Utils::PlainTextEdit.
>>>>
>>>> Perhaps I'm doing it wrong, i.e. I shouldn't take a hard dependency on the built-in editor being of a certain class.
>>>>
>>>> Say I were to write a new plugin to Qt Creator having only one command, like F5 in Notepad in Windows: inserting the current date and time where the text cursor is.
>>>>
>>>> What would the best approach be? (Utils::MacroExpander could probably be good enough for inserting the date but what about the time.)
>>>>
>>> Hi Henry,
>>>
>>> does it have to be exactly a plugin? In "Tools" -> "External" -> "Configure..." you can easily set up something which inserts the output of the system's "date" command (and also "time" on Windows).
>>>
>>> Cheers,
>>> Robert
>>>
>> Hi, well yes for inserting date/time an external tool surely would suffice,
>> but I was thinking more how you would write a new plugin today with similar functions lika my "real" one:
>> e.g. having a macro for inserting "QString("")" and moving the text cursor to the left 2 times (to begin typing inside the quotes)
>> or a macro for inserting comments, either # or // depending on what file type you are editing (c++ or CMake)
>>
>> So in general, requiring a bit more intelligence, like a plugin :-)
> Hi,
>
> the Lua scripting API is also available without writing an actual plugin, via Tools > Scripting.
> So a good new way of achieving your goal would be to use Tools > Scripting > New Script, write the script there, and to e.g. assign a keyboard shortcut to the generated action Lua.Scripts.<name>, or to use the locator "t" filter for triggering menu items ("t <yourscriptname>" or "t scripts <yourscriptname>" depending how unique you make it).
>
> The script for what you describe above would be
>
> T = require'TextEditor'
> local editor = T.currentEditor()
> local cursor = editor:cursor()
> cursor:insertText('QString("")')
> local mainC = cursor:mainCursor()
> mainC:movePosition(T.TextCursor.MoveOperation.Left, T.TextCursor.MoveMode.MoveAnchor, 2)
> cursor:replaceMainCursor(mainC) -- < doesn't exist yet
> editor:setCursor(cursor) -- < doesn't exist yet
>
> with the caveat that the API for setting text cursors isn't exposed yet... we are adding things as we go along.
> 656009: WIP: Lua: Add API for setting the cursor on text editors | https://codereview.qt-project.org/c/qt-creator/qt-creator/+/656009
>

Thanks for all good answers, that Lua scripting API looks really 
interesting and might be the best way forward for me.

In my c++ plugin I'm registering around 50 different 
keystrokes/shortcuts using ActionManager::registerAction() calls. If 
that will be possible to do from a single Lua source file, sign me up!

Answer to Andre's plans A,B,C,D: the plugin is here:
https://gitlab.com/tungware/geezeredit/-/blob/master/GeezerEdit.cpp
(it's free to copy).

About using auto instead of qobject_cast<>: the plugin was written in 
2013 and the auto functionality came with c++11 so I don't think the 
compiler support was fully baked at that time (a good excuse, right :-)

Rgrds Henry
P.S. That almost all code in my plugin has worked so well since 2013 is 
really a testament of the quality of Qt Creator's design and 
implementation. I remember a speed bump when it switched from 32-bit to 
64-bit builds but nothing that couldn't be fixed in an hour or so. Keep 
up the good work!



More information about the Qt-creator mailing list