Search code examples
qtqmlqt-creatorqt5qmllint

How to use qmllint in Qt Creator?


qmllint is a syntax checker for QML files written by KDAB which is shipped as a plugin with Qt 5.4. It's usage is based on command line like:

$ qmllint myFile.qml

Is it possible to use it directly in Qt Creator?


Solution

  • QtCreator

    You can actually set custom commands in QtCreator to be run without cluttering your qmake file manually because that will effect all the other people in your project, too.

    So, if you want to make sure that you only do it for yourself and not clutter it for others, using QtCreator's shiny GUI, I would suggest to follow this:

    • Projects (left pane)
    • Build & Run
    • Build Steps
    • Add Build Step

    Here is the screen how exactly you can set up the command with the corresponding arguments:

    enter image description here

    With QtCreator's GUI, you can easily change the order with the same concept without touching your project file should you prefer that. There are use cases for that like:

    • You would not want to run any steps, not even qmake, before the qml file is properly validated

    • You only have C++ files, so there is no such a thing as "linkage".

    • etc.

    qmake

    There are other "generic" approaches useful outside QtCreator, although you asked about this IDE, like putting the command into variables like:

    • QMAKE_PRE_LINK

      QMAKE_PRE_LINK = qmllint $$PWD/path/to/myFile.qml
      
    • QMAKE_POST_LINK

      QMAKE_POST_LINK = qmllint $$PWD/path/to/myFile.qml
      
    • System command execution from your qmake project file

      system("qmllint $$PWD/path/to/myFile.ml")
      
    • Adding custom targets with QMAKE_EXTRA_TARGETS

      qmllinttarget.commands = qmllint $$PWD/path/to/myFile.qml
      QMAKE_EXTRA_TARGETS += qmllinttarget