Qt encoding Style

Source: Internet
Author: User

 

Qt encoding Style

This is an overview of the coding practices we use when writing Qt code. Data is collected by mining Qt source code, forums, email lists, and collaboration with developers.

Indent

4 spaces

Space. Do not use TAB!

Variable Declaration

One Variable per line

Avoid short variable names as much as possible (such as "a", "rbarr", and "nughdeget ")

Single-character variables are only used in the count of temporary variables or loops.

Define variables when they are actually needed

 

?

 

// Wrong

Int a, B;

Char * c, * d;

 

// Correct

Int height;

Int width;

Char * nameOfThis;

Char * nameOfThat;

 

It must start with a lowercase character and start with an uppercase letter.

Avoid abbreviations

 

?

 

// Wrong

Short Cntr;

Char ITEM_DELIM = '\ T ';

 

// Correct

Short counter;

Char itemDelimiter = '\ T ';

 

The class name always starts with an uppercase letter. The public class starts with Q (QRgb), and the public function usually starts with q (qRgb ).

Blank

Use empty rows to properly group statements

Always use a blank line (do not empty multiple lines)

Always use a space before each keyword and braces

 

?

 

// Wrong

If (foo ){

}

 

// Correct

If (foo ){

}

 

Add a space between the pointer and reference in type and *, but not between *, and variable.

?

 

Char * x;

Const QString & myString;

Const char * const y = "hello ";

Blank before and after binary Operators

No blank after type conversion

Avoid C-style type conversion whenever possible

 

?

 

// Wrong

Char * blockOfMemory = (char *) malloc (data. size ());

 

// Correct

Char * blockOfMemory = reinterpret_cast <char *> (malloc (data. size ()));

 

Braces

Basic Principle: The left braces and statements are kept in the same line:

 

?

 

// Wrong

If (codec)

{

}

 

// Correct

If (codec ){

}

 

Exception: In Function Definition and class definition, the left braces always occupy a single row:

 

?

 

Static void foo (int g)

{

QDebug ("foo: % I", g );

}

 

Class Moo

{

};

 

No braces are used when only one row of the control statement body.

 

?

 

// Wrong

If (address. isEmpty ()){

Return false;

}

 

For (int I = 0; I <10; ++ I ){

QDebug ("% I", I );

}

 

// Correct

If (address. isEmpty ())

Return false;

 

For (int I = 0; I <10; ++ I)

QDebug ("% I", I );

 

Exception 1: If the parent statement contains multiple rows, braces are used.

 

?

 

// Correct

If (address. isEmpty () |! IsValid ()

|! Codec ){

Return false;

}

 

Exception 2: In the if-else structure, if there is one line that spans multiple rows, braces are used.

 

?

 

 

// Wrong

If (address. isEmpty ())

Return false;

Else {

QDebug ("% s", qPrintable (address ));

++ It;

}

 

// Correct

If (address. isEmpty ()){

Return false;

} Else {

QDebug ("% s", qPrintable (address ));

++ It;

}

 

// Wrong

If ()

If (B)

...

Else

...

 

// Correct

If (){

If (B)

...

Else

...

}

 

If the body of the control statement is empty, braces are used.

 

?

 

// Wrong

While ();

 

// Correct

While (){}

 

Parentheses

Use parentheses to group expressions

 

?

 

// Wrong

If (a & B | c)

 

// Correct

If (a & B) | c)

 

// Wrong

A + B & c

 

// Correct

(A + B) & c

 

Switch statement

Case and switch are in the same column

Each case must have a break (or partial URN) statement, or note that the break is not required.

 

?

 

Switch (myEnum ){

Case Value1:

DoSomething ();

Break;

Case Value2:

DoSomethingElse ();

// Fall through

Default:

DefaultHandling ();

Break;

}

 

Broken Line

Keep each line shorter than 100 characters. If necessary, the line is broken.

Put the end of a line with a comma, and place the operator at the beginning of a line. If your editor is too narrow, an operator placed at the end of the line is not easy to see.

 

?

 

// Correct

If (longExpression

+ OtherLongExpression

+ OtherOtherLongExpression ){

}

 

// Wrong

If (longExpression +

OtherLongExpression +

OtherOtherLongExpression)

{

}

 

Inheritance and keyword 'virtual'

When a new virtual function is implemented, the header file does not contain the virtual keyword.

General exceptions

If it makes your code look bad, you can break any rule.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.