Qt Coding Style
English Spanish 简体中文 Русский 日本語
[toc align_right="yes" depth="2"]
Qt Coding Style
This is an overview of the low-level coding conventions we use when writing Qt code.
See Coding-Conventions for the higher-level conventions.
The data has been gathered by mining the Qt sources, discussion forums, email threads and through collaboration of the developers.
Indentation
- 4 spaces are used for indentation
- Spaces, not tabs!
Declaring variables
- Declare each variable on a separate line
- Avoid short or meaningless names (e.g. “a”, “rbarr”, “nughdeget”)
- Single character variable names are only okay for counters and temporaries, where the purpose of the variable is obvious
- Wait when declaring a variable until it is needed
<br /> // Wrong<br /> int a, b;<br /> char *c, *d;
// Correct<br /> int height;<br /> int width;<br /> char *nameOfThis;<br /> char '''nameOfThat;<br />
Variables and functions start with a lower-case letter. Each consecutive word in a variable’s name starts with an upper-case letter
* Avoid abbreviations
<br /> // Wrong<br /> short Cntr;<br /> char ITEM_DELIM = '';
// Correct<br /> short counter;<br /> char itemDelimiter = '';<br />
- Classes always start with an upper-case letter. Public classes start with a ‘Q’ (QRgb) followed by an upper case letter. Public functions most often start with a ‘q’ (qRgb).
- Acronyms are camel-cased (e.g. QXmlStreamReader, not QXMLStreamReader).
Whitespace
- Use blank lines to group statements together where suited
- Always use only one blank line
- Always use a single space after a keyword and before a curly brace:
<br /> // Wrong<br /> if(foo){<br /> }
// Correct<br /> if (foo) {<br /> }<br />
- For pointers or references, always use a single space between the type and ‘’ or ‘&’, but no space between the ‘’ or ‘&’ and the variable name:
<br /> char '''x;<br /> const QString &myString;<br /> const char''' const y = "hello&quot;;<br />
- Surround binary operators with spaces
- No space after a cast
- Avoid C-style casts when possible
<br /> // Wrong<br /> char* blockOfMemory = (char* ) malloc(data.size());
// Correct<br /> char '''blockOfMemory = reinterpret_cast&lt;char'''>(malloc(data.size()));<br />
- Do not put multiple statements on one line
- By extension, use a new line for the body of a control flow statement:
<br /> // Wrong<br /> if (foo) bar();
// Correct<br /> if (foo)<br /> bar();<br />
Braces
- Use attached braces: The opening brace goes on the same line as the start of the statement. If the closing brace is followed by another keyword, it goes into the same line as well:
<br /> // Wrong<br /> if (codec)<br /> {<br /> }<br /> else<br /> {<br /> }
// Correct<br /> if (codec) {<br /> } else {<br /> }<br />
- Exception: Function implementations and class declarations always have the left brace on the start of a line:
<br /> static void foo(int g)<br /> {<br /> qDebug("foo: %i&quot;, g);<br /> }
class Moo<br /> {<br /> };<br />
- Use curly braces only when the body of a conditional statement contains more than one line:
<br /> // Wrong<br /> if (address.isEmpty()) {<br /> return false;<br /> }
for (int i = 0; i < 10; +''i) {<br /> qDebug("%i&quot;, i);<br /> }
<br /> // Correct<br /> if (address.isEmpty())<br /> return false;
<br /> for (int i = 0; i < 10;i)<br /> qDebug("%i&quot;, i);<br />
* Exception 1: Use braces also if the parent statement covers several lines / wraps:
<br /> // Correct<br /> if (address.isEmpty() || !isValid()<br /> || !codec) {<br /> return false;<br /> }<br />
* Exception 2: Brace symmetry: Use braces also in if-then-else blocks where either the if-code or the else-code covers several lines:
<br /> // Wrong<br /> if (address.isEmpty())<br /> return false;<br /> else {<br /> qDebug("%s&quot;, qPrintable(address));<br />it;<br /> }
<br /> // Correct<br /> if (address.isEmpty()) {<br /> return false;<br /> } else {<br /> qDebug("%s&quot;, qPrintable(address));<br />it;<br /> }
<br /> // Wrong<br /> if (a)<br /> if (b)<br /> …<br /> else<br /> …
<br /> // Correct<br /> if (a) {<br /> if (b)<br /> …<br /> else<br /> …<br /> }<br />
* Use curly braces when the body of a conditional statement is empty
<br /> // Wrong<br /> while (a);
<br /> // Correct<br /> while (a) {}<br />
h2. Parentheses
* Use parentheses to group expressions:
<br /> // Wrong<br /> if (a && b || c)
<br /> // Correct<br /> if ((a && b) || c)
<br /> // Wrong<br /> a'' b & c
// Correct<br /> (a + b) & c<br />
Switch statements
- The case labels are in the same column as the switch
- Every case must have a break (or return) statement at the end or a comment to indicate that there’s intentionally no break, unless another case follows immediately.
<br /> switch (myEnum) {<br /> case Value1:<br /> doSomething();<br /> break;<br /> case Value2:<br /> case Value3:<br /> doSomethingElse();<br /> // fall through<br /> default:<br /> defaultHandling();<br /> break;<br /> }<br />
Jump statements (break, continue, return, and goto)
- Do not put 'else' after jump statements:
<br /> // Wrong<br /> if (thisOrThat)<br /> return;<br /> else<br /> somethingElse();
// Correct<br /> if (thisOrThat)<br /> return;<br /> somethingElse();<br />
- Exception: If the code is inherently symmetrical, use of 'else' is allowed to visualize that symmetry
Line breaks
- Keep lines shorter than 100 characters; wrap if necessary
- Commas go at the end of wrapped lines; operators start at the beginning of the new lines. An operator at the end of the line is easy to miss if the editor is too narrow.
<br /> // Wrong<br /> if (longExpression +<br /> otherLongExpression +<br /> otherOtherLongExpression) {<br /> }
// Correct<br /> if (longExpression<br /> + otherLongExpression<br /> + otherOtherLongExpression) {<br /> }<br />