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.
1// Copyright (c) 2011-present The Bitcoin Core developers2// Distributed under the MIT software license, see the accompanying3// file COPYING or http://www.opensource.org/licenses/mit-license.php.5#include <qt/qrimagewidget.h>7#include <qt/guiutil.h>9#include <QApplication>10#include <QClipboard>11#include <QDrag>12#include <QFontDatabase>13#include <QMenu>14#include <QMimeData>15#include <QMouseEvent>16#include <QPainter>18#include <bitcoin-build-config.h> // IWYU pragma: keep20#ifdef USE_QRCODE21#include <qrencode.h>22#endif24QRImageWidget::QRImageWidget(QWidget* parent)25: QLabel(parent)26{27contextMenu = new QMenu(this);28contextMenu->addAction(tr("&Save Image…"), this, &QRImageWidget::saveImage);29contextMenu->addAction(tr("&Copy Image"), this, &QRImageWidget::copyImage);30}32bool QRImageWidget::setQR(const QString& data, const QString& text)33{34#ifdef USE_QRCODE35setText("");36if (data.isEmpty()) return false;38// limit length39if (data.length() > MAX_URI_LENGTH) {40setText(tr("Resulting URI too long, try to reduce the text for label / message."));41return false;42}44QRcode *code = QRcode_encodeString(data.toUtf8().constData(), 0, QR_ECLEVEL_L, QR_MODE_8, 1);46if (!code) {47setText(tr("Error encoding URI into QR Code."));48return false;49}51QImage qrImage = QImage(code->width + 8, code->width + 8, QImage::Format_RGB32);52qrImage.fill(0xffffff);53unsigned char *p = code->data;54for (int y = 0; y < code->width; ++y) {55for (int x = 0; x < code->width; ++x) {56qrImage.setPixel(x + 4, y + 4, ((*p & 1) ? 0x0 : 0xffffff));57++p;58}59}60QRcode_free(code);62const int qr_image_size = QR_IMAGE_SIZE + (text.isEmpty() ? 0 : 2 * QR_IMAGE_MARGIN);63QImage qrAddrImage(qr_image_size, qr_image_size, QImage::Format_RGB32);64qrAddrImage.fill(0xffffff);65{66QPainter painter(&qrAddrImage);67painter.drawImage(QR_IMAGE_MARGIN, 0, qrImage.scaled(QR_IMAGE_SIZE, QR_IMAGE_SIZE));69if (!text.isEmpty()) {70QRect paddedRect = qrAddrImage.rect();71paddedRect.setHeight(QR_IMAGE_SIZE + QR_IMAGE_TEXT_MARGIN);73QFont font = GUIUtil::fixedPitchFont();74font.setStretch(QFont::SemiCondensed);75font.setLetterSpacing(QFont::AbsoluteSpacing, 1);76const qreal font_size = GUIUtil::calculateIdealFontSize(paddedRect.width() - 2 * QR_IMAGE_TEXT_MARGIN, text, font);77font.setPointSizeF(font_size);79painter.setFont(font);80painter.drawText(paddedRect, Qt::AlignBottom | Qt::AlignCenter, text);81}82}84setPixmap(QPixmap::fromImage(qrAddrImage));86return true;87#else88setText(tr("QR code support not available."));89return false;90#endif91}93QImage QRImageWidget::exportImage()94{95if (!GUIUtil::HasPixmap(this)) {96return QImage();97}99return this->pixmap(Qt::ReturnByValue).toImage();100}102void QRImageWidget::mousePressEvent(QMouseEvent *event)103{104if (event->button() == Qt::LeftButton && GUIUtil::HasPixmap(this)) {105event->accept();106QMimeData *mimeData = new QMimeData;107mimeData->setImageData(exportImage());109QDrag *drag = new QDrag(this);110drag->setMimeData(mimeData);111drag->exec();112} else {113QLabel::mousePressEvent(event);114}115}117void QRImageWidget::saveImage()118{119if (!GUIUtil::HasPixmap(this))120return;121QString fn = GUIUtil::getSaveFileName(122this, tr("Save QR Code"), QString(),123/*: Expanded name of the PNG file format.124See: https://en.wikipedia.org/wiki/Portable_Network_Graphics. */125tr("PNG Image") + QLatin1String(" (*.png)"), nullptr);126if (!fn.isEmpty())127{128exportImage().save(fn);129}130}132void QRImageWidget::copyImage()133{134if (!GUIUtil::HasPixmap(this))135return;136QApplication::clipboard()->setImage(exportImage());137}139void QRImageWidget::contextMenuEvent(QContextMenuEvent *event)140{141if (!GUIUtil::HasPixmap(this))142return;143contextMenu->exec(event->globalPos());144}
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.