QT控件大全 三十 QGumPush

来源:互联网 发布:足控的软件 编辑:程序博客网 时间:2024/06/11 03:25

效果如图;


核心代码:

#ifndef QGUMPUSH_H#define QGUMPUSH_H#include <QWidget>#include <QtGui>#include <QtDesigner/QDesignerExportWidget>#include <math.h>class QDESIGNER_WIDGET_EXPORT QGumPush : public QWidget{    Q_OBJECT    Q_PROPERTY(QFont font READ font WRITE setFont);    Q_PROPERTY(QColor colorTx READ colorText WRITE setColorText);    Q_PROPERTY(QColor colorBt READ colorButton WRITE setColorButton);    Q_PROPERTY(QString text READ text WRITE setText);Q_PROPERTY(int x_txt READ XText WRITE setXText);Q_PROPERTY(int y_txt READ YText WRITE setYText);    QFont font() const    {        return fontText;    }    QColor colorText() const    {        return colText;    }    int XText() const    {        return x_tx;    }    int YText() const    {        return y_tx;    }    QColor colorButton() const    {        return colBut;    }    QString text() const    {        return textBut;    }public:    QGumPush(QWidget *parent = 0);    QSize minimumSizeHint() const;    QSize sizeHint() const;signals:    void pressed();    void released();public slots:    void setText(QString);    void setFont(QFont);    void setColorText(QColor);    void setColorButton(QColor);    void setXText(int);    void setYText(int);protected:    void paintEvent(QPaintEvent *);    void mousePressEvent(QMouseEvent *);    void mouseReleaseEvent(QMouseEvent *);    void mouseMoveEvent(QMouseEvent *);    void initValue();    void paintButton();    void paintText();private:    int x_tx;    int y_tx;    bool pushed;    bool up;    QString textBut;    QString family;    QColor colBut;    QColor colText;    QFont fontText;    QPointF m_pos;};#endif

#include "qgumpush.h"QGumPush::QGumPush(QWidget *parent) : QWidget(parent){    initValue();}void QGumPush::initValue(){    pushed = false;    up = false;    colBut = Qt::darkGray;    fontText = QFont("Arial", 20, QFont::Bold);    textBut = "Gum Button";    colText = Qt::red;    x_tx = 25;    y_tx = 55;}void QGumPush::paintEvent(QPaintEvent *){    paintButton();    paintText();}void QGumPush::paintButton(){    QPainter painter(this);    painter.setWindow(0, 0, 200, 100);    painter.setRenderHint(QPainter::Antialiasing);    if (pushed == false)    {        QColor light = Qt::white;        QColor dark = colBut.darker(150);        painter.setPen(QPen(colBut, 2, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin));        QLinearGradient linGrad(5, 100, 15, 100);        linGrad.setColorAt(0, dark);        linGrad.setColorAt(1, colBut);        linGrad.setSpread(QGradient::PadSpread);        painter.setBrush(linGrad);        QRectF border(5, 5, 190, 90);        painter.drawRoundRect(border, 15, 15);        QLinearGradient linGrad1(185, 100, 195, 100);        linGrad1.setColorAt(0, colBut);        linGrad1.setColorAt(1, dark);        linGrad1.setSpread(QGradient::PadSpread);        painter.setBrush(linGrad1);        QRectF border1(50, 5, 145, 90);        painter.drawRoundRect(border1, 15, 15);        emit released();    }    if (pushed == true)    {        QColor light = Qt::white;        QColor dark = colBut.darker(130);        painter.setPen(QPen(colBut, 2, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin));        QLinearGradient linGrad(5, 100, 15, 100);        linGrad.setColorAt(0, dark);        linGrad.setColorAt(1, colBut);        linGrad.setSpread(QGradient::PadSpread);        painter.setBrush(linGrad);        QRectF border(5, 5, 190, 90);        painter.drawRoundRect(border, 15, 15);        QLinearGradient linGrad1(185, 100, 195, 100);        linGrad1.setColorAt(0, colBut);        linGrad1.setColorAt(1, dark);        linGrad1.setSpread(QGradient::PadSpread);        painter.setBrush(linGrad1);        QRectF border1(50, 5, 145, 90);        painter.drawRoundRect(border1, 15, 15);        QRadialGradient radGrad(m_pos.x(), m_pos.y(), 150);        radGrad.setColorAt(0, dark);        radGrad.setColorAt(0.5, colBut);        radGrad.setColorAt(0.75, colBut.light(100));        radGrad.setSpread(QGradient::ReflectSpread);        painter.setBrush(radGrad);        QRectF border2(10, 7, 180, 85);        painter.drawRoundRect(border2, 15, 15);                emit pressed();    }}void QGumPush::paintText(){    QPainter painter(this);    painter.setWindow(0, 0, 200, 100);    painter.setRenderHint(QPainter::Antialiasing);    painter.setBrush(colText);    if (up == true)    {        QPainterPath path;        path.addText(x_tx, y_tx, fontText, textBut);        int m_radius = 70;        int m_intensity = 40;        double flip = m_intensity / 100.0;        for (int i=0; i<path.elementCount(); i++)        {            const QPainterPath::Element &e = path.elementAt(i);            double x = e.x;            double y = e.y;            double dx = x-m_pos.x();            double dy = y-m_pos.y();            double len = m_radius - sqrt(dx*dx + dy*dy);            if (len > 0)            {                path.setElementPositionAt(i, x+flip*dx*len/m_radius, y+flip*dy*len/m_radius);            }            else            {                path.setElementPositionAt(i, x, y);            }        }        painter.drawPath(path);        up = false;        update();    }    if (pushed == false)    {        QPainterPath path;        path.addText(x_tx, y_tx, fontText, textBut);        painter.drawPath(path);    }    if (pushed == true)    {        QPainterPath path;        path.addText(x_tx, y_tx, fontText, textBut);        int m_radius = 70;        int m_intensity = -30;        double flip = m_intensity / 100.0;        for (int i=0; i<path.elementCount(); i++)        {            const QPainterPath::Element &e = path.elementAt(i);            double x = e.x;            double y = e.y;            double dx = x-m_pos.x();            double dy = y-m_pos.y();            double len = m_radius - sqrt(dx*dx + dy*dy);            if (len > 0)            {                path.setElementPositionAt(i, x+flip*dx*len/m_radius, y+flip*dy*len/m_radius);            }            else            {                path.setElementPositionAt(i, x, y);            }        }        painter.drawPath(path);    }}void QGumPush::setText(QString text){    textBut = text;    update();}void QGumPush::setFont(QFont font){    fontText = font;    update();}void QGumPush::setColorText(QColor colorTx){    colText = colorTx;    update();}void QGumPush::setColorButton(QColor colorBt){    colBut = colorBt;    update();}void QGumPush::setXText(int x){    x_tx = x;    update();}void QGumPush::setYText(int y){    y_tx = y;    update();}QSize QGumPush::minimumSizeHint() const{    return QSize(20, 10);}QSize QGumPush::sizeHint() const{    return QSize(200, 100);}void QGumPush::mousePressEvent(QMouseEvent *e){    double x = e->x();    double y = e->y();    double wrun = width();    double hrun = height();    double wstart = 200;    double hstart = 100;    double dx = wrun/wstart;    double dy = hrun/hstart;    if (y>=(5*dy) && y<=(95*dy) && x>=(5*dx) && x<=(195*dx))    {        m_pos.setX(x/dx);        m_pos.setY(y/dy);        pushed = true;        update();    }}void QGumPush::mouseMoveEvent(QMouseEvent *e){    double x = e->x();    double y = e->y();    double wrun = width();    double hrun = height();    double wstart = 200;    double hstart = 100;    double dx = wrun/wstart;    double dy = hrun/hstart;    if (y>=(5*dy) && y<=(95*dy) && x>=(5*dx) && x<=(195*dx))    {        m_pos.setX(x/dx);        m_pos.setY(y/dy);        pushed = true;        update();    }}void QGumPush::mouseReleaseEvent(QMouseEvent *e){    double x = e->x();    double y = e->y();    double wrun = width();    double hrun = height();    double wstart = 200;    double hstart = 100;    double dx = wrun/wstart;    double dy = hrun/hstart;    if (y>=(5*dy) && y<=(95*dy) && x>=(5*dx) && x<=(195*dx))    {        m_pos.setX(x/dx);        m_pos.setY(y/dy);        pushed = false;        up = true;        update();    }}




原创粉丝点击