My first project when I entered KDAB was the migration of a multi-million lines Motif application to Qt... feels quite scary said like that. Fortunately, migrations from any toolkit to Qt is something KDAB has been doing from the beginning, and has lots of experience with.
You may be wondering what this has to do with automating tasks in Qt Creator, or why I have a new entry in my Qt Creator locator... keep up with me and everything will be clear shortly.
Automate all things
There are several rules you have to follow when doing a migration project, but the most important ones are probably those two:
Do not mix migration and refactoring
Automate all things
The second one is true for everything (remember, a good developer is a lazy developer), but it's particularly important when doing a migration, as you don't want to rewrite all single lines in a multi-million lines application by hand.
At that time, I was introduced to the wonderful world of XEmacs and Lisp scripting. During the course of the migration, I spent hours writing Lisp scripts to automate some parts of the migration, some with general purpose (used everywhere), others with a very narrow focus (only used once for one file). Coming from Windows and Microsoft Visual Studio 6 at the time, this was quite a new way to work for me.
Then slightly after this migration project, Qt Creator was released. As much as I like XEmacs and vim (and we all know which one is the best :) ), having a real IDE makes development so much easier, and Qt Creator was adopted quickly inside KDAB.
The road to automation in Qt Creator
Even if we are using Qt Creator, we still need to automate as much as possible, particularly when doing migration projects.
The Macro plugin
My first try was the creation of the macro plugin, contributed to Qt Creator in version 2.2.0. For those who know it, the name is inspired by the macro feature in Notepad++ (also a very nice editor at the time - Windows only). The plugin allows you to save and replay user inputs:
Start recording using Alt+[
Navigate, search, write code
Stop recording using Alt+]
Then replay the macro using Alt+R
If you want to reuse a macro between session, it's possible to save it, call it using the locator (the rm shortcut - "run macro" - probably not really used) or even assign a shortcut.
Though a nice step toward automation, it's still far from perfect, as a macro doesn't understand code semantics (it's only user inputs), and not everything can be recorded: for example completion is not recorded in the macro, as it's not a user input.
The Scripting plugin
Following the macro plugin, our second iteration was the creation of a plugin allowing us to write and run scripts, like you could do with Lisp and XEmacs. At the time, Qt Script was still nice and shiny, so it was based on it and the scripts were written in javascript. Like for macros, you can run scripts using the locator, or even assign shortcuts to useful scripts.
Here is a small script commenting out the function the cursor is in, while keeping the cursor at the same position:
var editor = editors.current()
var start = editor.createMark()
editor.gotoPosition(editor.currentFunction.start)
editor.find("{")
var startOfFunction = editor.position()
editor.gotoBlockEnd()
editor.gotoLineStart();
editor.insert("*/\n")
editor.gotoPosition(startOfFunction)
editor.insert("\n/*")
editor.gotoPosition(start)
As you can see, it's straightforward, and it has some semantic information of the code (editor.currentFunction for example). You have access to a wide range of APIs for scripting:
Access and handling of editors (text, C++ editors)
Code navigation
Files, file infos, directories
Semantic data
Git actions
...
It's actually way better than macros, and to be honest I now only use macros locally, I never save them anymore.
Lately, we reviewed the scripting plugin and checked what could be improved:
It is based on the dying Qt Script module
It's not possible to interact with the script
Back to the drawing board, coming from Qt Script/javascript the natural way forward is Qt Declarative/QML. Our new scripting plugin is now based on the QQmlEngine, and now supports user interactions.
A glimpse into our Qt Creator Scripting plugin
As said, the new plugin is now based on the QQmlEngine, opening a wide array of new features to us.
Script types
There are now two different script types one can use, depending on the needs:
javascript (*.js): automation without user interactions
QML (*.qml): automation, which allows us to add user interaction and visual UI
For the javascript scripts, it's close to what was possible with the previous scripting plugin based on Qt Script, the only difference is that we require a main function.
functionmain(){ message.log("This is a log line: message.log(...)")}
A QML script uses the same API, but allows more freedom, as you can now add a UI to it (using QtQuick) and/or add user interaction with the mouse and keyboard. Here is a small example of a script adding a header to the current file (it could have been done with a javscript script too, it's just an example):
import QtQml 2.2
import Script 1.0
Script{ property var mark
functioninit(){ mark = editor.createMark() editor.gotoDocumentStart() editor.insert("/****************************************************************************\n") editor.insert("/* Copyright (C) "+Qt.formatDate(newDate(),"yyyy")+" Klaralvdalens Datakonsult AB. All rights reserved.\n") editor.insert("** This file is part of the ACME project.") editor.insert("\n**********************************************************************/\n\n") m.restore()Qt.quit()}TextEditor{id:editorfileName:Editors.currentFileName}Component.onCompleted:init()}
User interaction
One of the benefits of having QML scripting is the availability of handling user interaction on an editor: as a script creator, you can easily handle key or mouse inputs on an editor:
Which ultimately leads you to write a game (actually 2: whack-a-mole and follow-path):
To watch this video on our website please or view it directly on YouTube
Conclusion
As someone said once: developers are lazy, and that's (usually) a good thing. It certainly took us some time to develop the different plugins, but it's paying off. It's also more fun to write a tool to automate the work than doing the work itself.
For now the scripting plugin is internal to KDAB, let us know if that's something you would be interested in.
You can download a KDAB whitepaper on Migrations here...
The KDAB Group is a globally recognized provider for software consulting, development and training, specializing in embedded devices and complex cross-platform desktop applications. In addition to being leading experts in Qt, C++ and 3D technologies for over two decades, KDAB provides deep expertise across the stack, including Linux, Rust and modern UI frameworks. With 100+ employees from 20 countries and offices in Sweden, Germany, USA, France and UK, we serve clients around the world.
9 Comments
23 - May - 2020
Bela TOTH
Hello,
This plugin seems very useful. Is it available / published / open-source?
I did not found any mention to it. (Nothing in open source QtCreator 4.12.1, nothing in KDAB's github repository, nothing the "Qt Creator Plug-in Gallery" Qt Wiki page.)
25 - May - 2020
Nicolas Arnaud-Cormos
Hello,
Thank you for your interest. The goal of this blog post was mostly to show the kind of internal tools we are using at KDAB.
The Scripting plugin is KDAB internal only for now, that's why you didn't find any mention of it. We are currently discussing internally what to do with it, but there are no decisions yet.
2 - Apr - 2021
Serg
We are currently discussing internally what to do with it, but there are no decisions yet.
Any chance the discussion is finished already and you could make us happy?
7 - Apr - 2021
Nicolas Arnaud-Cormos
The discussion is finished, and unfortunately we decided to not publish it, as the gain would be small for us and the burden to maintain it at our standard level high.
Sorry about that.
23 - May - 2020
Konstantin
Great idea. I would like to use it in practice.
25 - May - 2020
Nicolas Arnaud-Cormos
Hello,
Unfortunately the plugin is KDAB internal only. As answered previously, we are discussing internally what to do with it, but there are no decisions yet.
25 - Jun - 2020
E
Hello,
I have a question regarding this QT/QML, Can this platform do automatic processes in my windows? so for example; I have some files that I have to copy and paste from one folder to another- every week and then to start some SQL scripts and then also to use MsAccess, is it possible to do this work all automatic with this platform?
26 - Jun - 2020
Nicolas Arnaud-Cormos
Hello,
The task automation is really specific to writing code in Qt Creator. For what you want to do, you should look at writing powershell scripts.
30 - Sept - 2020
Luuk
This is something I have been looking for a long time. Too bad it is not (yet) available yet.
if (or when) it becomes available, please let me know.
Nicolas Arnaud-Cormos
Senior Software Engineer & Teamlead
Senior software engineer and teamlead at KDAB, Nicolas has actively developed with Qt since 2001 and is a founding member of Qtfr, the French Qt community site. He has worked on multiple Qt widgets or QML projects, with a particular emphasis on API design and software architecture. He has held Qt training classes for companies such as Michelin, Ford and ST-Ericsson. Nicolas holds an MSc in Computer Science.
9 Comments
23 - May - 2020
Bela TOTH
Hello,
This plugin seems very useful. Is it available / published / open-source? I did not found any mention to it. (Nothing in open source QtCreator 4.12.1, nothing in KDAB's github repository, nothing the "Qt Creator Plug-in Gallery" Qt Wiki page.)
25 - May - 2020
Nicolas Arnaud-Cormos
Hello, Thank you for your interest. The goal of this blog post was mostly to show the kind of internal tools we are using at KDAB. The Scripting plugin is KDAB internal only for now, that's why you didn't find any mention of it. We are currently discussing internally what to do with it, but there are no decisions yet.
2 - Apr - 2021
Serg
Any chance the discussion is finished already and you could make us happy?
7 - Apr - 2021
Nicolas Arnaud-Cormos
The discussion is finished, and unfortunately we decided to not publish it, as the gain would be small for us and the burden to maintain it at our standard level high. Sorry about that.
23 - May - 2020
Konstantin
Great idea. I would like to use it in practice.
25 - May - 2020
Nicolas Arnaud-Cormos
Hello, Unfortunately the plugin is KDAB internal only. As answered previously, we are discussing internally what to do with it, but there are no decisions yet.
25 - Jun - 2020
E
Hello,
I have a question regarding this QT/QML, Can this platform do automatic processes in my windows? so for example; I have some files that I have to copy and paste from one folder to another- every week and then to start some SQL scripts and then also to use MsAccess, is it possible to do this work all automatic with this platform?
26 - Jun - 2020
Nicolas Arnaud-Cormos
Hello, The task automation is really specific to writing code in Qt Creator. For what you want to do, you should look at writing powershell scripts.
30 - Sept - 2020
Luuk
This is something I have been looking for a long time. Too bad it is not (yet) available yet. if (or when) it becomes available, please let me know.