Skip to content

Instantly share code, notes, and snippets.

@juleswh
Last active July 9, 2019 13:28
Show Gist options
  • Save juleswh/aeacc89342bc51b19044cf1e04483357 to your computer and use it in GitHub Desktop.
Save juleswh/aeacc89342bc51b19044cf1e04483357 to your computer and use it in GitHub Desktop.
QtCreator cpp class wizard with headers complying with llvm style

Instead of the default (and pretty bugprone) qtcreator header guard CLASS_H, this defines a safer one in the form NAMESPACE_NESTED_NAMESPACES_CLASS_H. It is more likelly to comply with llvm-header-guard check of clang-tidy, if your namespaces matches your folder architecture.

Copy the three files (file.cpp file.h wizard.json) in your local qtcreator template folder:

$HOME/.config/QtProject/qtcreator/templates/wizards/classes/cpp/ on Linux and macOS or %APPDATA%\QtProject\qtcreator\templates\wizards\classes\cpp\ on Windows.

You may need to restart qtcreator for this to be considered.

They are a copy of the original ones from the QtCreator project, with only the line 20 in wizard.json changed:

{ "key": "GUARD", "value": "%{JS: Cpp.classToHeaderGuard('%{Class}', '%{JS: Util.suffix('%{HdrFileName}')}')}" },
%{Cpp:LicenseTemplate}\
#include "%{HdrFileName}"
%{JS: Cpp.openNamespaces('%{Class}')}
@if '%{IncludeQSharedData}'
class %{CN}Data : public QSharedData
{
public:
};
@endif
@if '%{Base}' === 'QObject'
%{CN}::%{CN}(QObject *parent) : QObject(parent)%{JS: ('%{SharedDataInit}') ? ', %{SharedDataInit}' : ''}
@elsif '%{Base}' === 'QWidget' || '%{Base}' === 'QMainWindow'
%{CN}::%{CN}(QWidget *parent) : %{Base}(parent)%{JS: ('%{SharedDataInit}') ? ', %{SharedDataInit}' : ''}
@else
%{CN}::%{CN}()%{JS: ('%{SharedDataInit}') ? ' : %{SharedDataInit}' : ''}
@endif
{
}
@if '%{IncludeQSharedData}'
%{CN}::%{CN}(const %{CN} &rhs) : data(rhs.data)
{
}
%{CN} &%{CN}::operator=(const %{CN} &rhs)
{
if (this != &rhs)
data.operator=(rhs.data);
return *this;
}
%{CN}::~%{CN}()
{
}
@endif
%{JS: Cpp.closeNamespaces('%{Class}')}\
%{Cpp:LicenseTemplate}\
#ifndef %{GUARD}
#define %{GUARD}
%{JS: QtSupport.qtIncludes([ ( '%{IncludeQObject}' ) ? 'QtCore/%{IncludeQObject}' : '',
( '%{IncludeQWidget}' ) ? 'QtGui/%{IncludeQWidget}' : '',
( '%{IncludeQMainWindow}' ) ? 'QtGui/%{IncludeQMainWindow}' : '',
( '%{IncludeQDeclarativeItem}' ) ? 'QtDeclarative/%{IncludeQDeclarativeItem}' : '',
( '%{IncludeQSharedData}' ) ? 'QtCore/QSharedDataPointer' : '' ],
[ ( '%{IncludeQObject}' ) ? 'QtCore/%{IncludeQObject}' : '',
( '%{IncludeQWidget}' ) ? 'QtWidgets/%{IncludeQWidget}' : '',
( '%{IncludeQMainWindow}' ) ? 'QtWidgets/%{IncludeQMainWindow}' : '',
( '%{IncludeQDeclarativeItem}' ) ? 'QtQuick1/%{IncludeQDeclarativeItem}' : '',
( '%{IncludeQQuickItem}' ) ? 'QtDeclarative/%{IncludeQQuickItem}' : '',
( '%{IncludeQSharedData}' ) ? 'QtCore/QSharedDataPointer' : '' ])}\
%{JS: Cpp.openNamespaces('%{Class}')}
@if '%{IncludeQSharedData}'
class %{CN}Data;
@endif
@if '%{Base}'
class %{CN} : public %{Base}
@else
class %{CN}
@endif
{
@if %{isQObject}
Q_OBJECT
@endif
public:
@if '%{Base}' === 'QObject'
explicit %{CN}(QObject *parent = nullptr);
@elsif '%{Base}' === 'QWidget' || '%{Base}' === 'QMainWindow'
explicit %{CN}(QWidget *parent = nullptr);
@else
%{CN}();
@endif
@if '%{IncludeQSharedData}'
%{CN}(const %{CN} &);
%{CN} &operator=(const %{CN} &);
~%{CN}();
@endif
@if %{isQObject}
signals:
public slots:
@endif
@if '%{IncludeQSharedData}'
private:
QSharedDataPointer<%{CN}Data> data;
@endif
};
%{JS: Cpp.closeNamespaces('%{Class}')}
#endif // %{GUARD}\
{
"version": 1,
"supportedProjectTypes": [ ],
"id": "A.Class",
"category": "O.C++",
"trDescription": "Creates a C++ header and a source file for a new class that you can add to a C++ project.",
"trDisplayName": "C++ Class",
"trDisplayCategory": "C++",
"iconText": "h/cpp",
"enabled": "%{JS: [ %{Plugins} ].indexOf('CppEditor') >= 0}",
"options":
[
{ "key": "TargetPath", "value": "%{Path}" },
{ "key": "HdrPath", "value": "%{Path}/%{HdrFileName}" },
{ "key": "SrcPath", "value": "%{Path}/%{SrcFileName}" },
{ "key": "CN", "value": "%{JS: Cpp.className('%{Class}')}" },
{ "key": "Base", "value": "%{JS: ( '%{BaseCB}' === '' ) ? '%{BaseEdit}' : '%{BaseCB}'}" },
{ "key": "isQObject", "value": "%{JS: [ 'QObject', 'QWidget', 'QMainWindow', 'QDeclarativeItem', 'QQuickItem'].indexOf('%{Base}') >= 0 }" },
{ "key": "GUARD", "value": "%{JS: Cpp.classToHeaderGuard('%{Class}', '%{JS: Util.suffix('%{HdrFileName}')}')}" },
{ "key": "SharedDataInit", "value": "%{JS: ('%{IncludeQSharedData}') ? 'data(new %{CN}Data)' : '' }" }
],
"pages":
[
{
"trDisplayName": "Define Class",
"trShortTitle": "Details",
"typeId": "Fields",
"data" :
[
{
"name": "Class",
"trDisplayName": "Class name:",
"mandatory": true,
"type": "LineEdit",
"data": { "validator": "(?:(?:[a-zA-Z_][a-zA-Z_0-9]*::)*[a-zA-Z_][a-zA-Z_0-9]*|)" }
},
{
"name": "BaseCB",
"trDisplayName": "Base class:",
"type": "ComboBox",
"data":
{
"items": [ { "trKey": "<Custom>", "value": "" },
"QObject", "QWidget", "QMainWindow", "QDeclarativeItem", "QQuickItem" ]
}
},
{
"name": "BaseEdit",
"type": "LineEdit",
"enabled": "%{JS: '%{BaseCB}' === ''}",
"mandatory": false,
"data":
{
"trText": "%{BaseCB}",
"trDisabledText": "%{BaseCB}"
}
},
{
"name": "Sp1",
"type": "Spacer",
"data": { "factor": 2 }
},
{
"name": "IncludeQObject",
"trDisplayName": "Include QObject",
"type": "CheckBox",
"data":
{
"checkedValue": "QObject",
"uncheckedValue": "",
"checked": "%{JS: '%{BaseCB}' === 'QObject'}"
}
},
{
"name": "IncludeQWidget",
"trDisplayName": "Include QWidget",
"type": "CheckBox",
"data":
{
"checkedValue": "QWidget",
"uncheckedValue": "",
"checked": "%{JS: '%{BaseCB}' === 'QWidget'}"
}
},
{
"name": "IncludeQMainWindow",
"trDisplayName": "Include QMainWindow",
"type": "CheckBox",
"data":
{
"checkedValue": "QMainWindow",
"uncheckedValue": "",
"checked": "%{JS: '%{BaseCB}' === 'QMainWindow'}"
}
},
{
"name": "IncludeQDeclarativeItem",
"trDisplayName": "Include QDeclarativeItem - Qt Quick 1",
"type": "CheckBox",
"data":
{
"checkedValue": "QDeclarativeItem",
"uncheckedValue": "",
"checked": "%{JS: '%{BaseCB}' === 'QDeclarativeItem'}"
}
},
{
"name": "IncludeQQuickItem",
"trDisplayName": "Include QQuickItem - Qt Quick 2",
"type": "CheckBox",
"data":
{
"checkedValue": "QQuickItem",
"uncheckedValue": "",
"checked": "%{JS: '%{BaseCB}' === 'QQuickItem'}"
}
},
{
"name": "IncludeQSharedData",
"trDisplayName": "Include QSharedData",
"type": "CheckBox",
"data":
{
"checkedValue": "QSharedData",
"uncheckedValue": "",
"checked": false
}
},
{
"name": "Sp2",
"type": "Spacer"
},
{
"name": "HdrFileName",
"type": "LineEdit",
"trDisplayName": "Header file:",
"mandatory": true,
"data": { "trText": "%{JS: Cpp.classToFileName('%{Class}', '%{JS: Util.preferredSuffix('text/x-c++hdr')}')}" }
},
{
"name": "SrcFileName",
"type": "LineEdit",
"trDisplayName": "Source file:",
"mandatory": true,
"data": { "trText": "%{JS: Cpp.classToFileName('%{Class}', '%{JS: Util.preferredSuffix('text/x-c++src')}')}" }
},
{
"name": "Path",
"type": "PathChooser",
"trDisplayName": "Path:",
"mandatory": true,
"data":
{
"kind": "existingDirectory",
"basePath": "%{InitialPath}",
"path": "%{InitialPath}"
}
}
]
},
{
"trDisplayName": "Project Management",
"trShortTitle": "Summary",
"typeId": "Summary"
}
],
"generators":
[
{
"typeId": "File",
"data":
[
{
"source": "file.h",
"target": "%{HdrPath}",
"openInEditor": true,
"options": [
{ "key": "Cpp:License:FileName", "value": "%{HdrFileName}" },
{ "key": "Cpp:License:ClassName", "value": "%{CN}" }
]
},
{
"source": "file.cpp",
"target": "%{SrcPath}",
"openInEditor": true,
"options": [
{ "key": "Cpp:License:FileName", "value": "%{SrcFileName}" },
{ "key": "Cpp:License:ClassName", "value": "%{CN}" }
]
}
]
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment