diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index 68c98cc..6648423 100644 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -701,38 +701,7 @@ void MainWindow::setupRecieveTab() { } ui->txtRecieve->setPlainText(addr); - - QSize sz = ui->qrcodeDisplay->size(); - - QPixmap pm(sz); - pm.fill(Qt::white); - QPainter painter(&pm); - - // NOTE: At this point you will use the API to get the encoding and format you want, instead of my hardcoded stuff: - qrcodegen::QrCode qr = qrcodegen::QrCode::encodeText(addr.toUtf8().constData(), qrcodegen::QrCode::Ecc::LOW); - const int s = qr.getSize()>0?qr.getSize():1; - const double w = sz.width(); - const double h = sz.height(); - const double aspect = w/h; - const double size = ((aspect>1.0)?h:w); - const double scale = size/(s+2); - const double offset = (w - size) > 0 ? (w - size) / 2 : 0; - // NOTE: For performance reasons my implementation only draws the foreground parts in supplied color. - // It expects background to be prepared already (in white or whatever is preferred). - painter.setPen(Qt::NoPen); - painter.setBrush(QColor(Qt::black)); - for(int y=0; yqrcodeDisplay->setPixmap(pm); + ui->qrcodeDisplay->setAddress(addr); }); } diff --git a/src/mainwindow.ui b/src/mainwindow.ui index 95dee31..e0f75ac 100644 --- a/src/mainwindow.ui +++ b/src/mainwindow.ui @@ -22,7 +22,7 @@ - 3 + 2 @@ -316,8 +316,8 @@ 0 0 - 849 - 369 + 841 + 321 @@ -670,7 +670,7 @@ - + 0 @@ -718,7 +718,7 @@ 0 0 889 - 19 + 22 @@ -783,6 +783,13 @@ + + + QRCodeLabel + QLabel +
qrcodelabel.h
+
+
tabWidget inputsCombo diff --git a/src/qrcodelabel.cpp b/src/qrcodelabel.cpp new file mode 100644 index 0000000..a556cd6 --- /dev/null +++ b/src/qrcodelabel.cpp @@ -0,0 +1,56 @@ +#include "qrcodelabel.h" + +QRCodeLabel::QRCodeLabel(QWidget *parent) : + QLabel(parent) +{ + this->setMinimumSize(1,1); + setScaledContents(false); +} + +QSize QRCodeLabel::sizeHint() const +{ + int w = this->width(); + return QSize(w, w); // 1:1 +} + +void QRCodeLabel::resizeEvent(QResizeEvent * e) +{ + if(!address.isEmpty()) + QLabel::setPixmap(scaledPixmap()); +} + +QPixmap QRCodeLabel::scaledPixmap() const { + QPixmap pm(size()); + pm.fill(Qt::white); + QPainter painter(&pm); + + qrcodegen::QrCode qr = qrcodegen::QrCode::encodeText(address.toUtf8().constData(), qrcodegen::QrCode::Ecc::LOW); + const int s = qr.getSize()>0?qr.getSize():1; + const double w = pm.width(); + const double h = pm.height(); + const double aspect = w/h; + const double size = ((aspect>1.0)?h:w); + const double scale = size/(s+2); + const double offset = (w - size) > 0 ? (w - size) / 2 : 0; + + // NOTE: For performance reasons my implementation only draws the foreground parts + painter.setPen(Qt::NoPen); + painter.setBrush(QColor(Qt::black)); + for(int y=0; y