Qt编写自定义控件:圆形颜色选择控件2

代码:

#ifndef COLORPALETTE_H
#define COLORPALETTE_H

#include <QWidget>

class colorPalette : public QWidget
{
    Q_OBJECT
public:
    explicit colorPalette(QWidget *parent = nullptr);

signals:
    void colorString(QString);
protected:
    void paintEvent(QPaintEvent *event)override;
    void mousePressEvent(QMouseEvent *event)override;
    void resizeEvent(QResizeEvent *event)override;

private:
    bool isPointInCir(const QPoint &point,const QRect & rect);//判断点是否在圆范围内
    QPoint pressPos;
    int radius;
    QRect boundingRect;
};

#endif // COLORPALETTE_H
#include "colorpalette.h"
#include <QMouseEvent>
#include <QDebug>
#include <QPainter>
#include "math.h"

colorPalette::colorPalette(QWidget *parent) : QWidget(parent)
{

}

void colorPalette::mousePressEvent(QMouseEvent *event)
{
    pressPos = event->pos();

    if(isPointInCir(pressPos,boundingRect))
    {
        QString strDecimalValue,strHex;
        QPixmap pixmap(this->size());
        this->render(&pixmap);

        if (!pixmap.isNull())
        {
            QImage image = pixmap.toImage();
            if (!image.isNull())
            {
                int x = pressPos.x();
                int y = pressPos.y();

                QColor color = image.pixel(x, y);
                int red = color.red();
                int green = color.green();
                int blue = color.blue();

                strDecimalValue = QString("%1, %2, %3").arg(red).arg(green).arg(blue);
                strHex = QString("#%1%2%3").arg(QString("%1").arg(red&0xFF,2,16,QLatin1Char('0')).toUpper())    //red&0xFF 数字转成16进制
                                           .arg(QString("%1").arg(green&0xFF,2,16,QLatin1Char('0')).toUpper())
                                           .arg(QString("%1").arg(blue&0xFF,2,16,QLatin1Char('0')).toUpper());
            }
        }
        qDebug()<<strDecimalValue<<pressPos;
        emit colorString(strDecimalValue);
        update();
    }

    QWidget::mousePressEvent(event);
}

void colorPalette::resizeEvent(QResizeEvent *event)
{
    pressPos = QPoint();
    QWidget::resizeEvent(event);
}

void colorPalette::paintEvent(QPaintEvent *event)
{
    QPainter painter(this);
    painter.setRenderHint(QPainter::Antialiasing,true);  //反走样开启
    const auto rect = event->rect();

    radius = std::min(rect.width(),rect.height()) / 2 - 5;
    boundingRect = QRect((rect.width() - 2*radius) / 2,(rect.height() - 2*radius) / 2,radius*2,radius*2);

    QConicalGradient conicalGradient(0, 0, 0); //创建渐变 前两个参数是渐变的中心点  第三个参数是渐变的角度

    QList<Qt::GlobalColor> list{Qt::red,
                                Qt::yellow,
                                Qt::green,
                                Qt::cyan,
                                Qt::blue,
                                Qt::magenta};

    for(int i = 0;i < list.size();++i)
    {
        conicalGradient.setColorAt(60.0 * i/360.0, list[i]);
    }
    conicalGradient.setColorAt(1.0, list[0]);

    painter.save();
    painter.translate(rect.center()); //将坐标系的原点设置为(r,r)
    QBrush brush(conicalGradient);
    painter.setPen(Qt::NoPen);
    painter.setBrush(brush);
    painter.drawEllipse(QPoint(0, 0), radius, radius);
    painter.restore();

    if(!pressPos.isNull())
    {
        painter.save();
        QPen pen;
        pen.setWidth(3);
        pen.setColor(Qt::white);
        painter.setPen(pen);
        painter.drawLine(QPoint(pressPos.x()-10,pressPos.y()),QPoint(pressPos.x()-7,pressPos.y()));
        painter.drawLine(QPoint(pressPos.x()+7,pressPos.y()),QPoint(pressPos.x()+10,pressPos.y()));

        painter.drawLine(QPoint(pressPos.x(),pressPos.y()-10),QPoint(pressPos.x(),pressPos.y()-7));
        painter.drawLine(QPoint(pressPos.x(),pressPos.y()+7),QPoint(pressPos.x(),pressPos.y()+10));

        painter.restore();
    }

    QWidget::paintEvent(event);
}

bool colorPalette::isPointInCir(const QPoint &point,const QRect & rect)
{
    const QPoint & centerPoint = rect.center();
    int x = point.x() - centerPoint.x();
    int y = point.y() - centerPoint.y();
    if(sqrt(pow(x,2) + pow(y,2)) <= (rect.width() / 2))
    {
        return true;
    }
    return false;
}

使用:

#include "colorpalette.h"
#include <QApplication>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QLabel>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);

    colorPalette * cpw = new colorPalette;
    QHBoxLayout * hb = new QHBoxLayout;

    QLabel * label = new QLabel("选择的颜色:");
    label->setAlignment(Qt::AlignRight | Qt::AlignVCenter);
    hb->addWidget(label);

    label = new QLabel;
    label->setFixedSize(40,40);
    hb->addWidget(label);
    colorPalette::connect(cpw,&colorPalette::colorString,[label](QString string)
    {
        label->setStyleSheet(QString("background-color: rgb(%1);").arg(string));
    });

    QVBoxLayout * vb = new QVBoxLayout;
    vb->addWidget(cpw);
    vb->addLayout(hb);

    QWidget w;
    w.resize(500,600);
    w.setLayout(vb);
    w.show();
    
    return a.exec();
}

效果:

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值