qt/main.cpp – Commented Bitcoin source code

qt/main.cpp – Commented Bitcoin source code

Having run the Bitcoin Core graphical interface for so many years, the GUI source code seemed like the perfect place to start the journey into Bitcoin Core. The messages, buttons and other strings are familiar, so it’ll help us find what does what and how the Qt layer talks to the Bitcoin subsystem.

(I could’ve chosen the bitcoind daemon instead, which would’ve lead us down a similar path through the code, but I’ve only peeked at the daemon code superficially at this point. We’ll turn to bitcoind ASAP in this series.)

Note: Throughout this series of posts all paths are assumed relative to the src/ directory in the Bitcoin Core source tree.

qt/main.cpp

This is bitcoin-qt’s entry point and it’s so short we can post the entire source here for discussion. It’s basically a stub that sets up the translation function and passes control directly to GuiMain

#include <qt/bitcoin.h>
#include <util/translation.h>
#include <QCoreApplication>
#include <functional>
#include <string>

/** Translate string to current locale using Qt. */extern const std::function<std::string(const char*)> G_TRANSLATION_FUN = [](const char* psz) {
return QCoreApplication::translate("bitcoin-core", psz).toStdString();
};

int main(int argc, char* argv[]) { return GuiMain(argc, argv); }

(Note: I skipped the MIT license notice, please do refer to the COPYING file for licensing details.)

The first included header, qt/bitcoin.h is, perhaps, the most important header in the Qt graphical interface source code.

We discuss qt/bitcoin.hat length in its own article #TODO#LINK.

#include <util/translation.h> is a utility header which implements a simple language translation mechanism. Struct bilingual_str is declared, containing two std::string fields (original and translated). A C++ standard library function object called G_TRANSLATION_FUN is declared and can be user-defined to treat translation as desired. A default implementation of G_TRANSLATION_FUN called bilingual_str is offered as fallback in case the user never defines it.

#include <QCoreApplication> is a standard Qt include which defines the QCoreApplication class. This class is used in the implementation of G_TRANSLATION_FUN. Otherwise no other Qt functionality happens in main.cpp

Lastly, main.cpp calls the default C and C++ entry function main():

int main(int argc, char* argv[]) { return GuiMain(argc, argv); }

This is where the program starts running.

All main() does is call GuiMain, passing the C/C++ standard parameters int argc, char* argv[]

GuiMain is declared in qt/bitcoin.h and defined in qt/bitcoin.cpp

 - On September 12, 2019