来源: http://blog.csdn.net/qq_35488967/article/details/70055490
// Wrong
int a, b;
char *c, *d;
// Correct
int height;
int width;
char *nameOfThis;
char *nameOfThat;
// Wrong
short Cntr;
char ITEM_DELIM = ‘ ‘;
// Correct
short counter;
char itemDelimiter = ‘ ‘;
// Wrong
if(foo){
}
// Correct
if (foo) {
}
char *x;
const QString &myString;
const char * const y = "hello";
QLineF lineF;
QLine lineN;
if (lineF == lineN) // Ok, lineN is implicitly converted to QLineF
if (lineN == lineF) // Error: QLineF cannot be converted implicitly to QLine, and the LHS is a member so no conversion applies
#include <QApplication>
#include <QMessageBox>
int main(int argc, char *argv[])
{
QT_REQUIRE_VERSION(argc, argv, "4.0.2")
QApplication app(argc, argv);
...
return app.exec();
}
struct Point2D
{
int x;
int y;
};
#include <QtGlobal>
#if (QT_VERSION >= QT_VERSION_CHECK(5, 0, 0))
#include <QtWidgets>
#else
#include <QtGui>
#endif
qDebug() << Q_FUNC_INFO << "was called with value1:" << value1 << "value2:" << value2;
QT_REQUIRE_VERSION(argc, argv, "4.0.2")
// Wrong
char* blockOfMemory = (char* ) malloc(data.size());
// Correct
char *blockOfMemory = reinterpret_cast<char *>(malloc(data.size()));
// Wrong
if (foo) bar();
// Correct
if (foo)
bar();
// Wrong
if (codec)
{
}
else
{
}
// Correct
if (codec) {
} else {
}
static void foo(int g)
{
qDebug("foo: %i", g);
}
class Moo
{
};
// 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);
// Correct
if (address.isEmpty() || !isValid()
|| !codec) {
return false;
}
// Wrong
if (address.isEmpty())
qDebug("empty!");
else {
qDebug("%s", qPrintable(address));
it;
}
// Correct
if (address.isEmpty()) {
qDebug("empty!");
} else {
qDebug("%s", qPrintable(address));
it;
}
// Wrong
if (a)
…
else
if (b)
…
// Correct
if (a) {
…
} else {
if (b)
…
}
// Wrong
while (a);
// Correct
while (a) {}
// Wrong
if (a && b || c)
// Correct
if ((a && b) || c)
// Wrong
a + b & c
// Correct
(a + b) & c
switch (myEnum) {
case Value1:
doSomething();
break;
case Value2:
case Value3:
doSomethingElse();
// fall through
default:
defaultHandling();
break;
}
// Wrong
if (thisOrThat)
return;
else
somethingElse();
// Correct
if (thisOrThat)
return;
somethingElse();
// Wrong
if (longExpression +
otherLongExpression +
otherOtherLongExpression) {
}
// Correct
if (longExpression
+ otherLongExpression
+ otherOtherLongExpression) {
}
--style=kr
--indent=spaces=4
--align-pointer=name
--align-reference=name
--convert-tabs
--attach-namespaces
--max-code-length=100
--max-instatement-indent=120
--pad-header
--pad-oper
int foo = some_really_long_function_name(and_another_one_to_drive_the_point_home(
first_argument, second_argument, third_arugment));
#include <qstring.h> // Qt class
#include <new> // STL stuff
#include <limits.h> // system stuff
#include <private/whatever_p.h>
QString s;
return condition ? s : "nothing"; // crash at runtime - QString vs. const char *
union AlignHelper {
char c;
int i;
};
// global scope
static const QString x; // Wrong - default constructor needs to be run to initialize x
static const QString y = "Hello"; // Wrong - constructor that takes a const char * has to be run
QString z; // super wrong
static const int i = foo(); // wrong - call time of foo() undefined, might not be called at all
// global scope
static const char x[] = "someText"; // Works - no constructor must be run, x set at compile time
static int y = 7; // Works - y will be set at compile time
static MyStruct s = {1, 2, 3}; // Works - will be initialized statically, no code being run
static QString *ptr = 0; // Pointers to objects are ok - no code needed to be run to initialize ptr
Q_GLOBAL_STATIC(QString, s)
void foo()
{
s()->append("moo");
}
char c; // c can‘t be negative if it is unsigned
/********/
/*******/
if (c > 0) { … } // WRONG - condition is always true on platforms where the default is unsigned
// 例如:A库有以下代码
class Q_EXPORT X: public QList<QVariant> {};
//B库有以下代码
class Q_EXPORT Y: public QList<QVariant> {};
这样,QList的符号就被导出了两次
for (Container::const_iterator it = c.begin(); it != c.end(); ++it) // W R O N G
for (Container::const_iterator it = c.cbegin(); it != c.cend(); ++it) // Right
static QObject *obj = 0;
if (!obj)
obj = new QObject(QCoreApplication::instance());
[static] bool qFuzzyCompare(double p1, double p2)
// Instead of comparing with 0.0
qFuzzyCompare(0.0,1.0e-200); // This will return false
// Compare adding 1 to both values will fix the problem
qFuzzyCompare(1 + 0.0, 1 + 1.0e-200); // This will return true
class B: public A
{
using A::val;
int val(int x);
};
#if Foo == 0 // W R O N G
#if defined(Foo) && (Foo == 0) // Right
#if Foo - 0 == 0 // Clever, are we? Use the one above instead, for better readability
public:
void setColor(const QColor& c);
QColor color() const;
void setDirty(bool b);
bool isDirty() const;
private Q_SLOTS:
void slotParentChanged();
//.h文件
class KFooPrivate;
class KFoo
{
public:
/* public members */
private:
const QScopedPointer<KFooPrivate> d;
};
//.cpp文件
class KFooPrivate
{
public:
int someInteger;
};
KFoo::KFoo() : d(new KFooPrivate)
{
/* ... */
}
KFoo::~KFoo()
{
// You must define a non-inline destructor in the .cpp file, even if it is empty
// else, a default one will be built in placed where KFooPrivate is only forward
// declare, leading to error in the destructor of QScopedPointer
}
static QString KApplication::makeStdCaption( const QString &caption,
bool withAppName,
bool modified);
class KApplication
{
public:
/* [...] */
enum StandardCaptionOption {
/**
* Indicates to include the application name
*/
WithApplicationName = 0x01,
/**
* Note in the caption that there is unsaved data
*/
Modified = 0x02
};
Q_DECLARE_FLAGS(StandardCaptionOptions,
StandardCaptionOption)
/**
* Builds a caption using a standard layout.
*
* @param userCaption The caption string you want to display
* @param options a set of flags from MakeStandartCaptionOption
*/
static QString makeStandardCaption(const QString& userCaption,
const StandardCaptionOptions& options = WithApplicationName);
/* [...] */
};
Q_DECLARE_OPERATORS_FOR_FLAGS(KApplication::StandardCaptionOptions)
QString myMethod( const QString& foo,
const QPixmap& bar,
int number );
const QList<int> &someProperty() const;
QList<int> someProperty() const;
#include <kfoobase.h>
class KBar;
class KFoo : public KFooBase
{
public:
/* [...] */
void myMethod(const KBar &bar);
};
#include <iostream>
#include <QDate>
#include <zlib.h>
#include "myclass.h"
//正确示例
#include <QDate> //correct
//错误示例
#include <QtCore/QDate> //incorrect, no need to specify the module QtCore
#include <QtCore> //incorrect, do not include the top-level Qt module
//.h文件
class Foo
{
public:
Bar getBar();
};
.cpp文件
#include "bar.h"
#include "foo.h"
#ifndef MYFOO_H
#define MYFOO_H
... <stuff>...
#endif /* MYFOO_H */
QObject::connect(this, SIGNAL( newValue(const QString&,
const MyNamespace::Type&) ),
other, SLOT( value(const QString &) ));
QObject::connect(this, SIGNAL(newValue(QString,MyNamespace::Type)),
other, SLOT(value(QString)));
void setAge(int age);
void setCategory(QChar cat);
void setName(QLatin1String name);
void setAlarm(const QSharedPointer<Alarm> &alarm); // const-ref is much faster than running copy-constructor and destructor
// QDate, QTime, QPoint, QPointF, QSize, QSizeF, QRect are good examples of other classes you should pass by value.
namespace Qt
{
enum Corner { TopLeft, BottomRight, … };
enum CaseSensitivity { Insensitive, Sensitive };
…
};
tabWidget->setCornerWidget(widget, Qt::TopLeft);
str.indexOf("$(QTDIR)", Qt::Insensitive);
namespace Qt
{
enum Corner { TopLeftCorner, BottomRightCorner, … };
enum CaseSensitivity { CaseInsensitive,
CaseSensitive };
…
};
tabWidget->setCornerWidget(widget, Qt::TopLeftCorner);
str.indexOf("$(QTDIR)", Qt::CaseInsensitive);
QSlider *slider = new QSlider(12, 18, 3, 13, Qt::Vertical, 0, "volume");
QSlider *slider = new QSlider(Qt::Vertical);
slider->setRange(12, 18);
slider->setPageStep(3);
slider->setValue(13);
slider->setObjectName("volume");
widget->repaint(false);
widget->repaint();
widget->repaint(true);
widget->repaint(false);
widget->repaint();
widget->repaintWithoutErasing();
str.replace("USER", user, false); // Qt 3
str.replace("USER", user, Qt::CaseInsensitive); // Qt 4
原文:http://www.cnblogs.com/lsgxeva/p/7873568.html