Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

How to auto format my code



  • I was looking through my options when it comes to auto formatting code with Qt Creator and so far I have been unsuccessful. I realize that I can press Ctrl-I in order to fix indentation issues but I am looking for more. I tried getting Clang Format:

    sudo apt-get install clang-format
    
    Help -> About plugins->Beautifier
    

    it told me to restart Qt so I did, then I set a macro:

    Tools -> Options -> Environment -> Keyboard
    

    I searched for: "Format Current File" and recorded my macro to be Ctrl Y

    then I went back to my code, messed with the formatting and then tried my new macro... nothing happened. So then I tried going back to Beautifier to try the different tools for the Enable auto format on file save option, none of which seemed to do anything to my code. The code I am attempting to format is qml, please help.



  • Hi @Circuits

    Below is the details how i did it in windows.
    Write the clang script as you wish to and include it in project. Or you can use the one available online for Qt

    # Clang-format configuration style to be used by Qt,
    # based on the rules from https://wiki.qt.io/Qt_Coding_Style and
    # https://wiki.qt.io/Coding_Conventions
    
    DISTFILES += \
          _clang-format
    

    Please also enable ClangCodeModule

    help -> About plugin ->  ClangCodeModule
    

    0_1558753285767_71af5026-fda7-429b-b5d8-805bdf11ca2e-image.png

    I Use LLVM for clang format and in Qt -> Tools -> Options -> Beautifier Set the Clang format command.

    0_1558753538255_66976584-0655-43dc-8aa6-fe2530f36970-image.png

    Then you can set the KeyBoard shortcut to it and it works.

    All the best.


  • Moderators

    @Circuits said in How to auto format my code:

    The code I am attempting to format is qml, please help.

    Beautifier is for C++ only.

    To do some simple auto formatting, either select all text and press Ctrl + i, or use reformat QML/JS option as described in https://forum.qt.io/topic/32185/qml-code-beautifier


  • Banned

    This post is deleted!


  • @sierdzio Ah I see, dang, I was hoping I could do extensive auto reformatting of qml code.


Log in to reply