![]() |
LibrePCB Developers Documentation
|
This page describes the code style guide for LibrePCB developers.
dev/CodingStyle_QtCreator.xml which you can import..clang-format file with the exact rules. You can use clang-format (version 15.0.7) to automatically format files according these rules. Use the command clang-format -style=file -i <FILE> to format a single source file../dev/format_code.sh. It will format all files which are modified compared to the master branch (this avoids formatting of files other than the ones you modified). For details, read the comment in that script.--docker argument.enum class, since C++11) whenever reasonablenullptr instead of NULL or 0const, constexpr, final, override, ... whenever possiblevoid foo() noexcept; // this method never throws exceptions
std::unique_ptr or std::shared_ptr for object ownership (instead of raw pointers). But never use QSharedPointer and try to avoid QScopedPointer for new code.QtCore, QtWidgets,...)lengthunit.cpp, lengthunit.hlibrepcb::project::editor::SchematicEditorState_Select)IF_. Example: librepcb::IF_GraphicsViewEventHandlermSomeMemberVariablesSomeStaticVariableqint32) instead of the types from <stdint.h> (for example int32_t).qreal instead of float or double whenever possible. This way the application should work also on ARM platforms with single precision FPU quite well.librepcb::Exception and derived classes), see [exceptions.h].std::exception).QObject only when needed (for example if you only need QObject::tr(), see note to Q_DECLARE_TR_FUNCTIONS() in Qt Macros)QObject::connect() with Qt's SIGNAL() and SLOT() macros. Use function addresses instead (whenever possible). This way, signals and slots are checked on compile-time instead of runtime. // Runtime check --> avoid this!
connect(&myTimer, SIGNAL(timeout()), this, SLOT(mySlot()));
// Compile-time check (and any method can be used as slots!) --> use this!
connect(&myTimer, &QTimer::timeout, this, &MyClass::anyMethod);
// Using C++11 lambda functions as slots is also possible.
// This can be very useful, but use it carefully (dangling pointers, ...)!
connect(&mAutoSaveTimer, &QTimer::timeout, [this](){doSomething();});
Q_CHECK_PTR() because it throws a std::bad_alloc exception which we will never catch (we only catch our own exception types). Use Q_ASSERT() instead.Q_DECLARE_TR_FUNCTIONS() is very useful to use the translation method QObject::tr() (resp. tr()) also in classes which do not inherit from QObject (but do NOT use this macro in interface classes because of possible multiple inheritance --> use QCoreApplication::translate() instead in this case). See http://doc.qt.io/qt-5/qcoreapplication.html#Q_DECLARE_TR_FUNCTIONS.qDebug(), qInfo(), qWarning() and qCritical().tr() for them.