Inside Bitcoin
Opening the selected file…
Inside Bitcoin
Opening the selected file…
The code behind the network, with room to understand it.
Release published 2026-10-28 · Snapshot fetched 2026-11-01. Latest stable release at fetch time; development branches and Git history are excluded. Only the selected code window and visible folders load here.
501QApplication::setAttribute(Qt::AA_DontUseNativeDialogs);502#endif504BitcoinApplication app;505GUIUtil::LoadFont(QStringLiteral(":/fonts/monospace"));507/// 2. Parse command-line options. We do this after qt in order to show an error if there are problems parsing these508// Command-line options take precedence:509SetupServerArgs(gArgs, init->canListenIpc());510SetupUIArgs(gArgs);511std::string error;512if (!gArgs.ParseParameters(argc, argv, error)) {513InitError(Untranslated(strprintf("Error parsing command line arguments: %s", error)));514// Create a message box, because the gui has neither been created nor has subscribed to core signals515QMessageBox::critical(nullptr, CLIENT_NAME,516// message cannot be translated because translations have not been initialized517QString::fromStdString("Error parsing command line arguments: %1.").arg(QString::fromStdString(error)));518return EXIT_FAILURE;519}521// Error out when loose non-argument tokens are encountered on command line522// However, allow BIP-21 URIs only if no options follow523bool payment_server_token_seen = false;524for (int i = 1; i < argc; i++) {525QString arg(argv[i]);526bool invalid_token = !arg.startsWith("-");527#ifdef ENABLE_WALLET528if (arg.startsWith(BITCOIN_IPC_PREFIX, Qt::CaseInsensitive)) {529invalid_token &= false;530payment_server_token_seen = true;531}532#endif533if (payment_server_token_seen && arg.startsWith("-")) {534InitError(Untranslated(strprintf("Options ('%s') cannot follow a BIP-21 payment URI", argv[i])));535QMessageBox::critical(nullptr, CLIENT_NAME,536// message cannot be translated because translations have not been initialized537QString::fromStdString("Options ('%1') cannot follow a BIP-21 payment URI").arg(QString::fromStdString(argv[i])));538return EXIT_FAILURE;539}540if (invalid_token) {541InitError(Untranslated(strprintf("Command line contains unexpected token '%s', see bitcoin-qt -h for a list of options.", argv[i])));542QMessageBox::critical(nullptr, CLIENT_NAME,543// message cannot be translated because translations have not been initialized544QString::fromStdString("Command line contains unexpected token '%1', see bitcoin-qt -h for a list of options.").arg(QString::fromStdString(argv[i])));545return EXIT_FAILURE;546}547}549// Now that the QApplication is setup and we have parsed our parameters, we can set the platform style550app.setupPlatformStyle();552/// 3. Application identification553// must be set before OptionsModel is initialized or translations are loaded,554// as it is used to locate QSettings555QApplication::setOrganizationName(QAPP_ORG_NAME);556QApplication::setOrganizationDomain(QAPP_ORG_DOMAIN);557QApplication::setApplicationName(QAPP_APP_NAME_DEFAULT);559/// 4. Initialization of translations, so that intro dialog is in user's language560// Now that QSettings are accessible, initialize translations561QTranslator qtTranslatorBase, qtTranslator, translatorBase, translator;562initTranslations(qtTranslatorBase, qtTranslator, translatorBase, translator);564// Show help message immediately after parsing command-line options (for "-lang") and setting locale,565// but before showing splash screen.566if (HelpRequested(gArgs) || gArgs.GetBoolArg("-version", false)) {567HelpMessageDialog help(nullptr, gArgs.GetBoolArg("-version", false));568help.showOrPrint();569return EXIT_SUCCESS;570}572// Install global event filter that makes sure that long tooltips can be word-wrapped573app.installEventFilter(new GUIUtil::ToolTipToRichTextFilter(TOOLTIP_WRAP_THRESHOLD, &app));575/// 5. Now that settings and translations are available, ask user for data directory576// User language is set up: pick a data directory577bool did_show_intro = false;578int64_t prune_MiB = 0; // Intro dialog prune configuration579// Gracefully exit if the user cancels580if (!Intro::showIfNeeded(did_show_intro, prune_MiB)) return EXIT_SUCCESS;582/// 6-7. Parse bitcoin.conf, determine network, switch to network specific583/// options, and create datadir and settings.json.584// - Do not call gArgs.GetDataDirNet() before this step finishes585// - Do not call Params() before this step586// - QSettings() will use the new application name after this, resulting in network-specific settings587// - Needs to be done before createOptionsModel588if (auto error = common::InitConfig(gArgs, ErrorSettingsRead)) {589InitError(error->message, error->details);590if (error->status == common::ConfigStatus::FAILED_WRITE) {591// Show a custom error message to provide more information in the592// case of a datadir write error.593ErrorSettingsWrite(error->message, error->details);594} else if (error->status != common::ConfigStatus::ABORTED) {595// Show a generic message in other cases, and no additional error596// message in the case of a read error if the user decided to abort.597QMessageBox::critical(nullptr, CLIENT_NAME, QObject::tr("Error: %1").arg(QString::fromStdString(error->message.translated)));598}599return EXIT_FAILURE;600}601#ifdef ENABLE_WALLET602// Parse URIs on command line603PaymentServer::ipcParseCommandLine(argc, argv);604#endif606QScopedPointer<const NetworkStyle> networkStyle(NetworkStyle::instantiate(Params().GetChainType()));607assert(!networkStyle.isNull());608// Allow for separate UI settings for testnets609QApplication::setApplicationName(networkStyle->getAppName());610// Re-initialize translations after changing application name (language in network-specific settings can be different)611initTranslations(qtTranslatorBase, qtTranslator, translatorBase, translator);613#ifdef ENABLE_WALLET614/// 8. URI IPC sending615// - Do this early as we don't want to bother initializing if we are just calling IPC616// - Do this *after* setting up the data directory, as the data directory hash is used in the name617// of the server.618// - Do this after creating app and setting up translations, so errors are619// translated properly.620if (PaymentServer::ipcSendCommandLine())621exit(EXIT_SUCCESS);623// Start up the payment server early, too, so impatient users that click on624// bitcoin: links repeatedly have their payment requests routed to this process:625if (WalletModel::isWalletEnabled()) {626app.createPaymentServer();627}628#endif // ENABLE_WALLET630/// 9. Main GUI initialization631// Install global event filter that makes sure that out-of-focus labels do not contain text cursor.632app.installEventFilter(new GUIUtil::LabelOutOfFocusEventFilter(&app));633#if defined(Q_OS_WIN)634// Install global event filter for processing Windows session related Windows messages (WM_QUERYENDSESSION and WM_ENDSESSION)635// Note: it is safe to call app.node() in the lambda below despite the fact636// that app.createNode() hasn't been called yet, because native events will637// not be processed until the Qt event loop is executed.638qApp->installNativeEventFilter(new WinShutdownMonitor([&app] { app.node().startShutdown(); }));639#endif640// Install qDebug() message handler to route to debug.log641qInstallMessageHandler(DebugMessageHandler);642// Allow parameter interaction before we create the options model643app.parameterSetup();644GUIUtil::LogQtInfo();646if (gArgs.GetBoolArg("-splash", DEFAULT_SPLASHSCREEN) && !gArgs.GetBoolArg("-min", false))647app.createSplashScreen(networkStyle.data());649app.createNode(*init);651// Load GUI settings from QSettings652if (!app.createOptionsModel(gArgs.GetBoolArg("-resetguisettings", false))) {653return EXIT_FAILURE;654}656if (did_show_intro) {657// Store intro dialog settings other than datadir (network specific)658app.InitPruneSetting(prune_MiB);659}661try662{663app.createWindow(networkStyle.data());664// Perform base initialization before spinning up initialization/shutdown thread665// This is acceptable because this function only contains steps that are quick to execute,666// so the GUI thread won't be held up.667if (app.baseInitialize()) {668app.requestInitialize();669#if defined(Q_OS_WIN)670WinShutdownMonitor::registerShutdownBlockReason(QObject::tr("%1 didn't yet exit safely…").arg(CLIENT_NAME), (HWND)app.getMainWinId());671#endif672app.exec();673} else {674// A dialog with detailed error will have been shown by InitError()675return EXIT_FAILURE;676}677} catch (const std::exception& e) {678PrintExceptionContinue(&e, "Runaway exception");679app.handleRunawayException(QString::fromStdString(app.node().getWarnings().translated));680} catch (...) {681PrintExceptionContinue(nullptr, "Runaway exception");682app.handleRunawayException(QString::fromStdString(app.node().getWarnings().translated));683}684return app.node().getExitStatus();685}
Independent explanations by MyBitcoinExplorer. Not affiliated with or endorsed by Bitcoin Core. Guides are educational and do not replace the source or its tests.
Bitcoin Core source © its contributors, distributed under the MIT license. Original notices and bundled third-party licenses are retained in the source tree.