@@ -30,18 +30,151 @@ If the results look stupid, fix the clang-format file if possible,
3030or disable clang-format in the affected area
3131using ` // clang-format off ` and ` // clang-format on ` .
3232
33+ #### Style preferences not caught by clang-format
34+ These are flexible. You can ignore them if it looks or works better to
35+ for one reason or another.
36+
37+ Use ` auto ` if the type of a variable can be deduced automatically, instead of
38+ redeclaring the returned value's type. Additionally, auto should be used when a
39+ constructor takes arguments.
40+
41+ ``` cpp
42+ auto x = <expr>; // ok
43+ auto x = QString::number(3 ); // ok
44+ QString x; // ok
45+ QString x = " foo" ; // ok
46+ auto x = QString(" foo" ); // ok
47+
48+ auto x = QString(); // avoid
49+ QString x (); // avoid
50+ QString x ("foo"); // avoid
51+ ```
52+
53+ Put newlines around logical units of code, and after closing braces. If the
54+ most reasonable logical unit of code takes only a single line, it should be
55+ merged into the next single line logical unit if applicable.
56+ ```cpp
57+ // multiple units
58+ auto x = <expr>; // unit 1
59+ auto y = <expr>; // unit 2
60+
61+ auto x = <expr>; // unit 1
62+ emit this->y(); // unit 2
63+
64+ auto x1 = <expr>; // unit 1
65+ auto x2 = <expr>; // unit 1
66+ auto x3 = <expr>; // unit 1
67+
68+ auto y1 = <expr>; // unit 2
69+ auto y2 = <expr>; // unit 2
70+ auto y3 = <expr>; // unit 2
71+
72+ // one unit
73+ auto x = <expr>;
74+ if (x...) {
75+ // ...
76+ }
77+
78+ // if more than one variable needs to be used then add a newline
79+ auto x = <expr>;
80+ auto y = <expr>;
81+
82+ if (x && y) {
83+ // ...
84+ }
85+ ```
86+
87+ Class formatting:
88+ ``` cpp
89+ // ! Doc comment summary
90+ // / Doc comment body
91+ class Foo : public QObject {
92+ // The Q_OBJECT macro comes first. Macros are ; terminated.
93+ Q_OBJECT;
94+ QML_ELEMENT;
95+ QML_CLASSINFO(...);
96+ // Properties must stay on a single line or the doc generator won't be able to pick them up
97+ Q_PROPERTY(...);
98+ /// Doc comment
99+ Q_PROPERTY(...);
100+ /// Doc comment
101+ Q_PROPERTY(...);
102+
103+ public:
104+ // Classes should have explicit constructors if they aren't intended to
105+ // implicitly cast. The constructor can be inline in the header if it has no body.
106+ explicit Foo(QObject* parent = nullptr): QObject(parent) {}
107+
108+ // Instance functions if applicable.
109+ static Foo* instance();
110+
111+ // Member functions unrelated to properties come next
112+ void function();
113+ void function();
114+ void function();
115+
116+ // Then Q_INVOKABLEs
117+ Q_INVOKABLE function();
118+ /// Doc comment
119+ Q_INVOKABLE function();
120+ /// Doc comment
121+ Q_INVOKABLE function();
122+
123+ // Then property related functions, in the order (bindable, getter, setter).
124+ // Related functions may be included here as well. Function bodies may be inline
125+ // if they are a single expression. There should be a newline between each
126+ // property's methods.
127+ [[ nodiscard]] QBindable<T > bindableFoo() { return &this->bFoo; }
128+ [[ nodiscard]] T foo() const { return this->foo; }
129+ void setFoo();
130+
131+ [[ nodiscard]] T bar() const { return this->foo; }
132+ void setBar();
133+
134+ signals:
135+ // Signals that are not property change related go first.
136+ // Property change signals go in property definition order.
137+ void asd();
138+ void asd2();
139+ void fooChanged();
140+ void barChanged();
141+
142+ public slots:
143+ // generally Q_INVOKABLEs are preferred to public slots.
144+ void slot();
145+
146+ private slots:
147+ // ...
148+
149+ private:
150+ // statics, then functions, then fields
151+ static const foo BAR;
152+ static void foo();
153+
154+ void foo();
155+ void bar();
156+
157+ // property related members are prefixed with ` m ` .
158+ QString mFoo;
159+ QString bar;
160+
161+ // Bindables go last and should be prefixed with ` b ` .
162+ Q_OBJECT_BINDABLE_PROPERTY(Foo, QString, bFoo, &Foo::fooChanged);
163+ };
164+ ```
165+
33166### Linter
34167All contributions should pass the linter.
35168
36169Note that running the linter requires disabling precompiled
37170headers and including the test codepaths:
38171```sh
39172$ just configure debug -DNO_PCH=ON -DBUILD_TESTING=ON
40- $ just lint
173+ $ just lint-changed
41174```
42175
43176If the linter is complaining about something that you think it should not,
44- please disable the lint in your MR and explain your reasoning.
177+ please disable the lint in your MR and explain your reasoning if it isn't obvious .
45178
46179### Tests
47180If you feel like the feature you are working on is very complex or likely to break,
@@ -77,7 +210,7 @@ and list of headers to scan for documentation.
77210### Commits
78211Please structure your commit messages as ` scope[!]: commit ` where
79212the scope is something like ` core ` or ` service/mpris ` . (pick what has been
80- used historically or what makes sense if new.) Add ` ! ` for changes that break
213+ used historically or what makes sense if new). Add ` ! ` for changes that break
81214existing APIs or functionality.
82215
83216Commit descriptions should contain a summary of the changes if they are not
0 commit comments