Qt自定义颜色对话框QColorDialog

QColorDialog使用起来并不灵活,我们可以自定义颜色对话框

版本:qt6.5
套件:MSVC2019
框架:MainWindow

先放代码,后面介绍思路
.h代码

(直接在头文件中实现函数,这样是为了方便移植和浏览)


#include <QMainWindow>
#include<QApplication>

#include<QFrame>
#include<QPainter>
#include<QWidgetAction>
#include<QPushButton>
#include<QGridLayout>
#include<QMouseEvent>
#include<QLabel>
#include<qpixmap>
#include<QSpinBox>
#include<QTextEdit>
#include<QSignalBlocker>
#include<QString>
#include<QRegularExpression>
#include<QLineEdit>
#include<QMessageBox>
#include<QScreen>
#include<QTimer>



class ColorSelectedLineEdit : public QLineEdit
{
    Q_OBJECT
public:
    ColorSelectedLineEdit(QWidget *parent = nullptr): QLineEdit(parent){};
    ~ColorSelectedLineEdit(){};
protected:
    void keyPressEvent(QKeyEvent *event) override
    {
        if (event->key() == Qt::Key_Return) {
            // 在这里完成编辑的逻辑
            this->setText(this->text());
            this->clearFocus();
            emit textChanged(text());
        } else {
            QLineEdit::keyPressEvent(event);
        }
    }
};


class SVSelectLabel : public QLabel
{
    Q_OBJECT
public:
    SVSelectLabel(QWidget *parent = nullptr,int m_width=256,int m_height=256):QLabel(parent),width(m_width),height(m_height)
    {
        this->setFixedSize(QSize(width,height));

        pen.setColor(QColor(255,255,255));pen.setWidth(1);

        int sv_width=this->getWidth();
        int sv_height=this->getHeight();
        backupImg=new QImage(sv_width,sv_height,QImage::Format_RGB32);
        QColor sv_hsvColor;
        // 遍历图像的每个像素
        for (int x = 0; x < sv_width; x++) {
            for (int y = 0; y < sv_height; y++) {
                sv_hsvColor.setHsv(360,x,y);
                backupImg->setPixelColor(x,y,sv_hsvColor);
            }
        }
        backupPixmap=QPixmap::fromImage(*backupImg).copy();
        this->setPixmap(backupPixmap);
    };
    ~SVSelectLabel(){};

    void pixmapChange(int h)
    {
        h=(int)((h+1)*1.40625)-1;
        int sv_width=this->width;
        int sv_height=this->height;
        if(h<0){h=0;}

        QColor sv_hsvColor;
        // 遍历图像的每个像素
        for (int x = 0; x < sv_width; x++) {
            for (int y = 0; y < sv_height; y++) {
                sv_hsvColor.setHsv(h,x,y);
                backupImg->setPixelColor(x,y,sv_hsvColor);
            }
        }

        backupPixmap=QPixmap::fromImage(*backupImg);
        this->setPixmap(backupPixmap);

        tempPixmap=this->backupPixmap.copy();
        painter.begin(&tempPixmap);
        painter.setPen(pen);
        painter.drawEllipse(selectedpPos,5,5);
        this->setPixmap(tempPixmap);
        painter.end();
    };//由h更改导致sv区域全部重绘

    void setWidth(int m_width){this->width=m_width;};
    void setHeight(int m_height){this->height=m_height;};
    int getWidth(){return width;}
    int getHeight(){return height;}

    QImage* backupImg;//备份修改前的图片
    QPixmap backupPixmap;//备份修改前的图片

private:
    int width;
    int height;
    bool isPress=false;
    QPoint selectedpPos;


    QPainter painter;
    QPen pen;

    QPixmap tempPixmap;

    // QWidget interface
protected:
    void mousePressEvent(QMouseEvent *event) override
    {
        if((event->pos().x()>=0)&&(event->pos().x()<width)&&(event->pos().y()>=0)&&(event->pos().y()<height))
        {
            selectedpPos= event->pos();
            isPress=true;emit svChange(event->pos());
        }

    };
    void mouseMoveEvent(QMouseEvent *event) override
    {
        if(isPress&&(event->pos().x()>=0)&&(event->pos().x()<width)&&(event->pos().y()>=0)&&(event->pos().y()<height))
        {
            selectedpPos= event->pos();
            emit svChange(event->pos());
        }
    };
    void mouseReleaseEvent(QMouseEvent *event) override
    {
        isPress=false;
    };

signals:
    void svChange(QPoint mousePos);
};




class HSelectLabel : public QLabel
{
    Q_OBJECT
public:
    HSelectLabel(QWidget *parent = nullptr,int m_width=30,int m_height=256):QLabel(parent),width(m_width),height(m_height)
    {
        this->setFixedSize(QSize(width,height));

        backupImg=new QImage(m_width,m_height,QImage::Format_RGB32);
        QColor h_hsvColor;
        // 遍历图像的每个像素
        for (int y = 0; y < m_height; y++) {
            for (int x = 0; x < m_width; x++) {
                h_hsvColor.setHsl((int)((y+1)*1.40625)-1,255,128);
                backupImg->setPixelColor(x,y,h_hsvColor);
            }
        }
        backupPixmap=QPixmap::fromImage(*backupImg)
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值