Skip to content

Automating tasks in Qt Creator

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.

Qt Creator Locator showing the Run Script selected

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.

This plugin is used for our Photon to Qt or MFC to Qt migrations.

The Scripting plugin… but better

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.

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()
}

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:

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):

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…

About KDAB

If you like this article and want to read similar material, consider subscribing via our RSS feed.

Subscribe to KDAB TV for similar informative short video content.

KDAB provides market leading software consulting and development services and training in Qt, C++ and 3D/OpenGL. Contact us.

FacebookTwitterLinkedInEmail

Categories: KDAB Blogs / KDAB on Qt / Qt / Technical

9 thoughts on “Automating tasks in Qt Creator”

  1. 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.)

    1. 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.

      1. > 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?

        1. 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.

    1. 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.

  2. 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?

    1. 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.

  3. 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.

Leave a Reply

Your email address will not be published. Required fields are marked *