Qt to write custom controls 26- tiled background

I. Introduction

Tile background control, the main application scenario is a canvas appear black and white background, and the like can be placed above the graphic image, so that the look better, such as after a new software layer PS background, canvas new software subsequent FireWorks transparent background, ICO authoring software after new canvas background, will adopt a black and white background. Although I use QPainter many years later only to find QPainter actually comes with this function draws a tiled background, wiping read QPainter own function, what he called drawTiledPixmap, Qt cross-platform GUI development is indeed in the crowd, these things were all taken into account, when it comes to thoughtful consideration, Qt in even the size and count and length are perfectly encapsulates, for different people's habits, this consideration is very thoughtful. drawTiledPixmap on two parameters, the first parameter is a region to be drawn, the second parameter is to draw pictures, pictures copied automatically fill insufficient, so if two alternating colors to provide a background image, will in turn draw effect of forming tiled background, so that the color to be controlled, this control is provided to increase the alternating colors, two colors can pass on their own as alternating colors, images generated automatically be drawn in the internal procedures.

Second, the realization of functions

  • 1: the background color may be provided alternately

Third, renderings

Fourth, the header file code

#ifndef TILEDBG_H
#define TILEDBG_H

/**
 * 平铺背景控件 作者:feiyangqingyun(QQ:517216493) 2018-8-25
 * 1:可设置交替背景颜色
 */

#include <QWidget>

#ifdef quc
#if (QT_VERSION < QT_VERSION_CHECK(5,7,0))
#include <QtDesigner/QDesignerExportWidget>
#else
#include <QtUiPlugin/QDesignerExportWidget>
#endif

class QDESIGNER_WIDGET_EXPORT TiledBg : public QWidget
#else
class TiledBg : public QWidget
#endif

{
    Q_OBJECT
    Q_PROPERTY(QColor color1 READ getColor1 WRITE setColor1)
    Q_PROPERTY(QColor color2 READ getColor2 WRITE setColor2)
    Q_PROPERTY(QPixmap bgPix READ getBgPix WRITE setBgPix)

public:
    explicit TiledBg(QWidget *parent = 0);

protected:
    void drawBg();
    void paintEvent(QPaintEvent *);

private:
    QColor color1;              //颜色1
    QColor color2;              //颜色2
    QPixmap bgPix;              //背景图片

public:
    QColor getColor1()          const;
    QColor getColor2()          const;
    QPixmap getBgPix()          const;

    QSize sizeHint()            const;
    QSize minimumSizeHint()     const;

public slots:
    //设置颜色1
    void setColor1(const QColor &color1);
    //设置颜色2
    void setColor2(const QColor &color2);
    //设置背景图片
    void setBgPix(const QPixmap &bgPix);
};

#endif // TILEDBG_H

Fifth, the core code

#pragma execution_character_set("utf-8")

#include "tiledbg.h"
#include "qpainter.h"
#include "qdebug.h"

TiledBg::TiledBg(QWidget *parent) : QWidget(parent)
{
    color1 = QColor(255, 255, 255);
    color2 = QColor(220, 220, 220);
    bgPix = QPixmap(64, 64);
    drawBg();
}

void TiledBg::drawBg()
{
    bgPix.fill(color1);
    QPainter painter(&bgPix);
    painter.fillRect(0, 0, 32, 32, color2);
    painter.fillRect(32, 32, 32, 32, color2);
    painter.end();
    update();
}

void TiledBg::paintEvent(QPaintEvent *)
{
    QPainter painter(this);
    painter.drawTiledPixmap(this->rect(), bgPix);
}

QColor TiledBg::getColor1() const
{
    return this->color1;
}

QColor TiledBg::getColor2() const
{
    return this->color2;
}

QPixmap TiledBg::getBgPix() const
{
    return this->bgPix;
}

QSize TiledBg::sizeHint() const
{
    return QSize(100,100);
}

QSize TiledBg::minimumSizeHint() const
{
    return QSize(20,20);
}

void TiledBg::setColor1(const QColor &color1)
{
    if (this->color1 != color1) {
        this->color1 = color1;
        drawBg();
    }
}

void TiledBg::setColor2(const QColor &color2)
{
    if (this->color2 != color2) {
        this->color2 = color2;
        drawBg();
    }
}

void TiledBg::setBgPix(const QPixmap &bgPix)
{
    this->bgPix = bgPix;
    update();
}

Sixth, the controls described

  1. More than 149 exquisite control, covers a variety of dashboards, progress bar, the progress of the ball, compass, graphs, scales, thermometers, navigation bar, navigation bar, flatui, highlight the button, slide the selector, the lunar calendar and so on. Qwt far more than the number of controls integration.
  2. Each class can be independently as a separate control, zero coupling each control file and a header file to achieve a code amount, independent of other files to facilitate individual control integrated into the project source code form, less. qwt interlocking control class, highly coupled, want to use one of the controls, must contain all the code.
  3. Write all pure Qt, QWidget + QPainter to draw, to support any Qt version Qt4.6 Qt5.12, support for mingw, msvc, gcc compiler, etc., support any operating system such as windows + linux + mac + embedded linux, which does not garbled can be directly integrated into Qt Creator, a built-in controls and use the same, most of the effects can be as long as several properties are set, very convenient.
  4. DEMO separate source containing the control corresponding to each control has a convenient reference. It also provides integrated use of all controls a DEMO.
  5. Source code for each control has detailed Chinese annotation, are prepared in accordance with unified design specifications, easy to learn to write custom controls.
  6. Each control default color and demo corresponding color is very beautiful.
  7. More than 130 visible control, six invisible control.
  8. Portion control provides a variety of styles style selection, multiple choice style indicator.
  9. All controls changes adaptive stretched form.
  10. Integrated design custom attribute that supports drag design, WYSIWYG support the import and export in xml format.
  11. Activex control that comes with demo, all controls can be run directly in the browser ie.
  12. Fontawesome integrated graphics font + Alibaba iconfont collection of hundreds of graphic fonts, font fun graphic brings.
  13. All controls and finally generate a dll dynamic library files, can be integrated directly into qtcreator designed for use in drag.
  14. Already qml version, the latter will consider a pyqt version, if the user is in great demand then.

Seven, SDK download

  • SDK download link: https://pan.baidu.com/s/1A5Gd77kExm8Co5ckT51vvQ extraction code: 877p
  • Download link is included in the various versions of the dynamic library files, header files of all the controls, the use of demo, custom controls + properties designer.
  • Open plug-in custom dynamic library dll use (permanent free), and the back door without any restrictions, ease of use.
  • 26 now available version dll, which includes qt5.12.3 msvc2017 32 + 64 mingw 32 + 64 in.
  • From time to time to increase control and improve controls, regularly updated SDK, to welcome all suggestions, thank you!
  • widget versions (QQ: 517216493) qml versions (QQ: 373955953) sambong camel (QQ: 278969898).
  • Qt's advanced column know almost Taoge road https://zhuanlan.zhihu.com/TaoQt
  • Welcome concern public micro-channel number] [efficient programmers, content C ++ / Python, learning, writing skills, popular technology, career development, a lot of dry goods, benefits a lot!

Guess you like

Origin www.cnblogs.com/feiyangqingyun/p/11184321.html