I wrote a Qt QR Code Display Control, qtqr.

Source: Internet
Author: User

I wrote a Qt QR Code Display Control, qtqr.

A recent project needs to display the QR code, so it took some time (only one night, but not perfect) to write a widget that displays the QR code. Of course, this control uses some open-source code, such as qrencode, so I plan to open-source my code.

My code references

Http://stackoverflow.com/questions/21400254/how-to-draw-a-qr-code-with-qt-in-native-c-c

It is basically written according to the above ideas.

First download libqrencode, which is a library generated by QR code in c language. The QR Code can contain 7000 numbers or 4000 characters. It can carry a large amount of information and is easy to use. For more details about QR Code, google.

Libqrencode currently only supports QR Code model 2. If you need ECI or FNC1 mode, you need to find another method.

I used MSYS2 to compile Libqrencode. If I directly configure, I encountered a small problem. The error is as follows:

...checking for pkg-config... nochecking for strdup... yeschecking for pthread_mutex_init in -lpthread... yeschecking for png... noconfigure: error: in `/home/Ivan/qrencode-3.4.4':configure: error: The pkg-config script could not be found or is too old.  Make sure itis in your PATH or set the PKG_CONFIG environment variable to the fullpath to pkg-config.Alternatively, you may set the environment variables png_CFLAGSand png_LIBS to avoid the need to call pkg-config.See the pkg-config man page for more details.To get pkg-config, see 

The general meaning is that my compiling environment does not have pkg-config, but it does not matter. According to the author, Libqrencode does not depend on any third-party library. Add the -- without-tools parameter to configure to pass the configuration.

 

After compilation, generate libqrencode. a In the. lib directory, and add the qrencode. h files. My Qt development environment is VS2010 + Qt4.5.1. Libqrencode. a can also be used in VS2010. In addition, the libwinpthread. dll. a file is required because some libpthread functions are used in Libqrencode.

In addition, after testing, the libqrencode is generated here. a still has some problems in VS2010, as it runs normally in Debug mode, but once the program is compiled into Release mode, it cannot run. It seems that libqrencode needs to be compiled using vs2010. It is estimated that it is not that simple. Wait for a while.

In the config. h file

/* Define to 1 if using pthread is enabled .*/
# Define HAVE_LIBPTHREAD 1

Changed:

// # Define HAVE_LIBPTHREAD 1

The libpthread dependency can be removed, and the compiled library files can be used in the release mode of vc2010.


The control I wrote is very simple. For more information, see the code.

#ifndef QRWIDGET_H#define QRWIDGET_H#include <QWidget>#include "qrencode.h"class QRWidget : public QWidget{    Q_OBJECTpublic:    explicit QRWidget(QWidget *parent = 0);    ~QRWidget();    void setString(QString str);    int getQRWidth() const;    bool saveImage(QString name, int size);private:    void draw(QPainter &painter, int width, int height);    QString string;    QRcode *qr;signals:protected:    void paintEvent(QPaintEvent *);    QSize sizeHint() const;    QSize minimumSizeHint() const;public slots:};#endif // QRWIDGET_H

#include "qrwidget.h"#include <QPainter>#include <QImage>QRWidget::QRWidget(QWidget *parent) : QWidget(parent){    qr = NULL;    setString("Hello QR Code");}QRWidget::~QRWidget(){    if(qr != NULL)    {        QRcode_free(qr);    }}int QRWidget::getQRWidth() const{    if(qr != NULL)    {        return qr->width;    }    else    {        return 0;    }}void QRWidget::setString(QString str){    string = str;    if(qr != NULL)    {        QRcode_free(qr);    }    qr = QRcode_encodeString(string.toStdString().c_str(),                             1,                             QR_ECLEVEL_L,                             QR_MODE_8,                             1);    update();}QSize QRWidget::sizeHint()  const{    QSize s;    if(qr != NULL)    {        int qr_width = qr->width > 0 ? qr->width : 1;        s = QSize(qr_width * 4, qr_width * 4);    }    else    {        s = QSize(50, 50);    }    return s;}QSize QRWidget::minimumSizeHint()  const{    QSize s;    if(qr != NULL)    {        int qr_width = qr->width > 0 ? qr->width : 1;        s = QSize(qr_width, qr_width);    }    else    {        s = QSize(50, 50);    }    return s;}bool QRWidget::saveImage(QString fileName, int size){    if(size != 0 && !fileName.isEmpty())    {        QImage image(size, size, QImage::Format_Mono);        QPainter painter(&image);        QColor background(Qt::white);        painter.setBrush(background);        painter.setPen(Qt::NoPen);        painter.drawRect(0, 0, size, size);        if(qr != NULL)        {            draw(painter, size, size);        }        return image.save(fileName);    }    else    {        return false;    }}void QRWidget::draw(QPainter &painter, int width, int height){    QColor foreground(Qt::black);    painter.setBrush(foreground);    const int qr_width = qr->width > 0 ? qr->width : 1;    double scale_x = width / qr_width;    double scale_y = height / qr_width;    for( int y = 0; y < qr_width; y ++)    {        for(int x = 0; x < qr_width; x++)        {            unsigned char b = qr->data[y * qr_width + x];            if(b & 0x01)            {                QRectF r(x * scale_x, y * scale_y, scale_x, scale_y);                painter.drawRects(&r, 1);            }        }    }}void QRWidget::paintEvent(QPaintEvent *){    QPainter painter(this);    QColor background(Qt::white);    painter.setBrush(background);    painter.setPen(Qt::NoPen);    painter.drawRect(0, 0, width(), height());    if(qr != NULL)    {        draw(painter, width(), height());    }}

The following is the software interface:


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.