#include "codewidget.h" CodeWidget::CodeWidget(QWidget *parent) : QWidget(parent) { resize(180, 160); casesen = true; margin = 10; percent = 0.23; foreground = QColor("black"); background = QColor("white"); mode = MODE_8; level = LEVEL_Q; } void CodeWidget::paintEvent(QPaintEvent *) { QPainter painter(this); QRcode *qrcode = QRcode_encodeString(text.data(), 7, (QRecLevel)level, (QRencodeMode)mode, casesen ? 1 : 0); if(0 != qrcode) { unsigned char *point = qrcode->data; painter.setPen(Qt::NoPen); painter.setBrush(this->background); painter.drawRect(0, 0, this->width(), this->height()); double scale = (this->width () - 2.0 * margin) / qrcode->width; painter.setBrush(this->foreground); for (int y = 0; y < qrcode->width; y ++) { for (int x = 0; x < qrcode->width; x ++) { if (*point & 1) { QRectF r(margin + x * scale, margin + y * scale, scale, scale); painter.drawRects(&r, 1); } point ++; } } point = NULL; QRcode_free(qrcode); // draw icon // if (icon.isNull ()) { painter.setBrush(this->background); double icon_width = (this->width () - 2.0 * margin) * percent; double icon_height = icon_width; double wrap_x = (this->width () - icon_width) / 2.0; double wrap_y = (this->width () - icon_height) / 2.0; QRectF wrap(wrap_x - 5, wrap_y - 5, icon_width + 10, icon_height + 10); painter.drawRoundRect(wrap, 50, 50); QPixmap image(":/new/prefix1/code.png"); QRectF target(wrap_x, wrap_y, icon_width, icon_height); QRectF source(0, 0, image.width (), image.height ()); painter.drawPixmap (target, image, source); } qrcode = NULL; } void CodeWidget::updateCode(QByteArray &text) { this->text = text; }