Sign up for the KDAB Newsletter
Stay on top of the latest news, publications, events and more.
Go to Sign-up
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.
There are several rules you have to follow when doing a migration project, but the most important ones are probably those two:
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.
Even if we are using Qt Creator, we still need to automate as much as possible, particularly when doing migration projects.
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:
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.
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:
It's actually way better than macros, and to be honest I now only use macros locally, I never save them anymore.
This plugin is used for our Photon to Qt or MFC to Qt migrations.
Lately, we reviewed the scripting plugin and checked what could be improved:
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.
As said, the new plugin is now based on the QQmlEngine, opening a wide array of new features to us.
There are now two different script types one can use, depending on the needs:
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.
function main() {
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
function init() {
mark = editor.createMark()
editor.gotoDocumentStart()
editor.insert("/****************************************************************************\n")
editor.insert("/* Copyright (C) " + Qt.formatDate(new Date(), "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: editor
fileName: Editors.currentFileName
}
Component.onCompleted: init()
}
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:
import QtQml 2.2
import Script 1.0
TextEditor {
id: editor
fileName: Util.mktemp("foo_XXXXXX.txt")
KeyHandler {
key: Qt.Key_Left
filter: true
onKeyPressed: doSomething()
}
MouseHandler {
onClicked: doSomethingElse()
}
// ...
}
Which ultimately leads you to write a game (actually 2: whack-a-mole and follow-path):
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...
About KDAB
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.
Stay on top of the latest news, publications, events and more.
Go to Sign-up
Upgrade your applications from Qt 5 to Qt 6 with KDAB’s migration services. Get a free migration assessment and join a hands-on workshop to prepare your team for a successful transition!
Learn more
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.