Qt World Summit: Submit your Presentation


Custom line-indentations with qmlformat.exe



  • I was very excited to read that Qt 5.15.0 includes the new qmlformat.exe tool, and naturally I have been attempting to use it to format the QML files within my codebase.

    However, I noticed that this formatter tool sets all indentation as 4 spaces. The entire rest of my codebase (i.e. the C++ files, header files, JS files, etc.) all use 2 space indentations.

    Is there a way to tell qmlformat.exe to use 2 space indentations?

    There is surprisingly no documentation (as of 01-July-2020) on the Qt Forums regarding how to use this tool, and running qmlformat.exe --help does not provide an answer to setting custom line indentations.

    Any help would be welcomed!



  • Hi, your question is one year old but I want to add a comment with hope it will get some attention. I am also happy that qmlformat is part of 5.15 version and also want to set custom indentation size.

    From what I saw, QmlJS formatter supports custom sizes, however CLI does not provide an option to set. Does anybody know a way how to set it up without the need to build qmlformat separately?

    Until this will be resolved, we still need to use 3rd party like https://github.com/jesperhh/qmlfmt or https://github.com/Orange-OpenSource/qmljsreformatter



  • Hi @JustASimpleCoder, I received a quick response from Qt staff, it seems that Qt6.2 version of qmlformat contains what we need! :)

    ./qmlformat -h                                       
    Usage: ./qmlformat [options] filenames
    Formats QML files according to the QML Coding Conventions.
    
    Options:
      -h, --help                  Displays help on commandline options.
      --help-all                  Displays help including Qt specific options.
      -v, --version               Displays version information.
      -V, --verbose               Verbose mode. Outputs more detailed information.
      -i, --inplace               Edit file in-place instead of outputting to
                                  stdout.
      -f, --force                 Continue even if an error has occurred.
      -t, --tabs                  Use tabs instead of spaces.
      -w, --indent-width <width>  How many spaces are used when indenting.
      -n, --normalize             Reorders the attributes of the objects according
                                  to the QML Coding Guidelines.
      -F, --files <file>          Format all files listed in file, in-place
      -l, --newline <newline>     Override the new line format to use (native macos
                                  unix windows).
    
    Arguments:
      filenames                   files to be processed by qmlformat