[Qt-creator] R.I.P. for QPlainTextEdit in Qr Creator 17?
Eike Ziller
Eike.Ziller at qt.io
Wed Jun 25 10:53:23 CEST 2025
> On 24. Jun 2025, at 17:04, Henry Skoglund <henry at tungware.se> wrote:
>
> On 2025-06-24 16:36, Robert Löhning via Qt-creator 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
Br, Eike
--
Eike Ziller
Principal Software Engineer
The Qt Company GmbH
Erich-Thilo-Str. 10
12489 Berlin, Germany
eike.ziller at qt.io
https://qt.io
Geschäftsführer: Mika Pälsi,
Juha Varelius, Jouni Lintunen
Sitz der Gesellschaft: Berlin, Registergericht: Amtsgericht Charlottenburg, HRB 144331 B
More information about the Qt-creator
mailing list