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)
最低0.47元/天 解锁文章
1001

被折叠的 条评论
为什么被折叠?



