English Spanish 简体中文 Русский 日本語

Qt Coding Style

This is an overview of the coding conventions we use when writing Qt code.

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

  1.         // Wrong
  2.         int a, b;
  3.         char *c, *d;
  4.        
  5.         // Correct
  6.         int height;
  7.         int width;
  8.         char *nameOfThis;
  9.         char *nameOfThat;

  • 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

  1.         // Wrong
  2.         short Cntr;
  3.         char ITEM_DELIM = '\t';
  4.        
  5.         // Correct
  6.         short counter;
  7.         char itemDelimiter = '\t';

  • 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).
  • Abbreviations 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.

  1.         // Wrong
  2.         if(foo){
  3.         }
  4.        
  5.         // Correct
  6.         if (foo) {
  7.         }

  • For pointers or references, always use a single space between the type and ‘*’ or ‘&’, but no space between the ‘*’ or ‘&’ and the variable name.

  1.         char *x;
  2.         const QString &myString;
  3.         const char * const y = "hello";

  • Surround binary operators with spaces.
  • No space after a cast.
  • Avoid C-style casts when possible.

  1.         // Wrong
  2.         char* blockOfMemory = (char* ) malloc(data.size());
  3.        
  4.         // Correct
  5.         char *blockOfMemory = reinterpret_cast<char *>(malloc(data.size()));

Braces

  • As a base rule, the left curly brace goes on the same line as the start of the statement:

  1.         // Wrong
  2.         if (codec)
  3.         {
  4.         }
  5.        
  6.         // Correct
  7.         if (codec) {
  8.         }

  • Exception: Function implementations and class declarations always have the left brace on the start of a line:

  1.         static void foo(int g)
  2.         {
  3.             qDebug("foo: %i", g);
  4.         }
  5.        
  6.         class Moo
  7.         {
  8.         };

  • Use curly braces when the body of a conditional statement contains more than one line, and also if a single line statement is somewhat complex.

  1.         // Wrong
  2.         if (address.isEmpty()) {
  3.             return false;
  4.         }
  5.        
  6.         for (int i = 0; i < 10; ++i) {
  7.             qDebug("%i", i);
  8.         }
  9.        
  10.         // Correct
  11.         if (address.isEmpty())
  12.             return false;
  13.        
  14.         for (int i = 0; i < 10; ++i)
  15.             qDebug("%i", i);

  • Exception 1: Use braces also if the parent statement covers several lines / wraps

  1.         // Correct
  2.         if (address.isEmpty() || !isValid()
  3.             || !codec) {
  4.             return false;
  5.         }

  • Exception 2: Use braces also in if-then-else blocks where either the if-code or the else-code covers several lines

  1.         // Wrong
  2.         if (address.isEmpty())
  3.             return false;
  4.         else {
  5.             qDebug("%s", qPrintable(address));
  6.             ++it;
  7.         }
  8.        
  9.         // Correct
  10.         if (address.isEmpty()) {
  11.             return false;
  12.         } else {
  13.             qDebug("%s", qPrintable(address));
  14.             ++it;
  15.         }
  16.        
  17.         // Wrong
  18.         if (a)
  19.             if (b)
  20.                 ...
  21.             else
  22.                 ...
  23.        
  24.         // Correct
  25.         if (a) {
  26.             if (b)
  27.                 ...
  28.             else
  29.                 ...
  30.         }

  • Use curly braces when the body of a conditional statement is empty

  1.         // Wrong
  2.         while (a);
  3.        
  4.         // Correct
  5.         while (a) {}

Parentheses

  • Use parentheses to group expressions:

  1.         // Wrong
  2.         if (a && b || c)
  3.        
  4.         // Correct
  5.         if ((a && b) || c)
  6.        
  7.         // Wrong
  8.         a + b & c
  9.        
  10.         // Correct
  11.         (a + b) & c

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.

  1.         switch (myEnum) {
  2.         case Value1:
  3.             doSomething();
  4.             break;
  5.         case Value2:
  6.         case Value3:
  7.             doSomethingElse();
  8.             // fall through
  9.         default:
  10.             defaultHandling();
  11.             break;
  12.         }

Line breaks

  • Keep lines shorter than 100 characters; insert breaks if necessary.
  • Commas go at the end of a broken line; operators start at the beginning of the new line. An operator at the end of the line is easy to not see if your editor is too narrow.

  1.         // Correct
  2.         if (longExpression
  3.             + otherLongExpression
  4.             + otherOtherLongExpression) {
  5.         }
  6.        
  7.         // Wrong
  8.         if (longExpression +
  9.             otherLongExpression +
  10.             otherOtherLongExpression) {
  11.         }

Inheritance and the `virtual` keyword

  • When reimplementing a virtual method, do not put the `virtual` keyword in the header file.

General exception

  • Feel free to break a rule if it makes your code look bad.

Categories: