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).copy();
this->setPixmap(backupPixmap);
}
~HSelectLabel(){};
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;
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 hChange(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 hChange(event->pos());
}
};
void mouseReleaseEvent(QMouseEvent *event) override
{
isPress=false;
};
signals:
void hChange(QPoint mousePos);
};
class ColorSelectedFrame : public QFrame
{
Q_OBJECT
public:
ColorSelectedFrame(QWidget *parent = nullptr):QFrame(parent)
{
this->setStyleSheet("ColorSelectedFrame{background-color:rgb(80,80,80);}");
sv_lbl=new SVSelectLabel(this,256,256);sv_lbl->move(2,2);
h_lbl=new HSelectLabel(this,30,256);h_lbl->move(275,2);
this->setFixedSize(470,262);
size_lbl.setWidth(26);size_lbl.setHeight(26);
size_sB.setWidth(44);size_sB.setHeight(26);
pen_ellipse.setColor(QColor(255,255,255));pen_ellipse.setWidth(2);
pen_rect.setColor(QColor(255,255,255));pen_rect.setWidth(2);
QString topTBr_sB_sB="QSpinBox{border:1px solid rgb(55,55,55);border-radius: 3px;background: transparent;selection-color: rgb(100,100,100);selection-background-color: rgb(180,180,180);font-size:8pt;color:rgb(240,240,240);font-family:Tw Cen MT;}";
QString topTBr_sB_hover="QSpinBox:hover{background: transparent;}";
QString topTBr_sB_upBtn="QSpinBox::up-button {border-image: url(:/src/UI/up.png);height:10px;margin-top:1px;}";
QString topTBr_sB_downBtn="QSpinBox::down-button {border-image: url(:/src/UI/down.png);height:10px;margin-bottom:1px;}";
QString topTBr_sB_style=topTBr_sB_sB+topTBr_sB_hover+topTBr_sB_upBtn+topTBr_sB_downBtn;
QString lbl_lbl="QLabel{color: rgb(250, 250, 250);margin-left:6px;}";
QString lbl_style=lbl_lbl;
lbl_h=new QLabel("h",this);lbl_h->setFixedSize(size_lbl);lbl_h->move(320,10);
sB_h=new QSpinBox(this);sB_h->setFixedSize(size_sB);sB_h->move(340,10);sB_h->setFocusPolicy(Qt::ClickFocus);
lbl_s=new QLabel("s",this);lbl_s->setFixedSize(size_lbl);lbl_s->move(320,70);
sB_s=new QSpinBox(this);sB_s->setFixedSize(size_sB);sB_s->move(340,70);sB_s->setFocusPolicy(Qt::ClickFocus);
lbl_v=new QLabel("v",this);lbl_v->setFixedSize(size_lbl);lbl_v->move(320,130);
sB_v=new QSpinBox(this);sB_v->setFixedSize(size_sB);sB_v->move(340,130);sB_v->setFocusPolicy(Qt::ClickFocus);
lbl_r=new QLabel("r",this);lbl_r->setFixedSize(size_lbl);lbl_r->move(400,10);
sB_r=new QSpinBox(this);sB_r->setFixedSize(size_sB);sB_r->move(420,10);sB_r->setFocusPolicy(Qt::ClickFocus);
lbl_g=new QLabel("g",this);lbl_g->setFixedSize(size_lbl);lbl_g->move(400,70);
sB_g=new QSpinBox(this);sB_g->setFixedSize(size_sB);sB_g->move(420,70);sB_g->setFocusPolicy(Qt::ClickFocus);
lbl_b=new QLabel("b",this);lbl_b->setFixedSize(size_lbl);lbl_b->move(400,130);
sB_b=new QSpinBox(this);sB_b->setFixedSize(size_sB);sB_b->move(420,130);sB_b->setFocusPolicy(Qt::ClickFocus);
this->sB_h->setStyleSheet(topTBr_sB_style);
this->sB_s->setStyleSheet(topTBr_sB_style);
this->sB_v->setStyleSheet(topTBr_sB_style);
this->sB_r->setStyleSheet(topTBr_sB_style);
this->sB_g->setStyleSheet(topTBr_sB_style);
this->sB_b->setStyleSheet(topTBr_sB_style);
QFont font_lbl;font_lbl.setFamily("Tw Cen MT");
this->lbl_h->setStyleSheet(lbl_style);this->lbl_h->setFont(font_lbl);
this->lbl_s->setStyleSheet(lbl_style);this->lbl_s->setFont(font_lbl);
this->lbl_v->setStyleSheet(lbl_style);this->lbl_v->setFont(font_lbl);
this->lbl_r->setStyleSheet(lbl_style);this->lbl_r->setFont(font_lbl);
this->lbl_g->setStyleSheet(lbl_style);this->lbl_g->setFont(font_lbl);
this->lbl_b->setStyleSheet(lbl_style);this->lbl_b->setFont(font_lbl);
lbl_nowColor=new QLabel(this);lbl_nowColor->setFixedSize(size_lbl);lbl_nowColor->move(320,180);
lineText_color=new ColorSelectedLineEdit(this);lineText_color->setFixedSize(QSize(100,26));lineText_color->move(360,179);lineText_color->setFocusPolicy(Qt::ClickFocus);
this->lineText_color->setStyleSheet("ColorSelectedLineEdit{border: 1px solid rgb(50,50,50);background-color:rgb(50,50,50);selection-background-color: rgb(180,180,180);color:rgb(250,250,250);}");
lineText_color->setFont(QFont("Tw Cen MT"));
connect(sv_lbl,SIGNAL(svChange(QPoint)),this,SLOT(on_svChange(QPoint)));
connect(h_lbl,SIGNAL(hChange(QPoint)),this,SLOT(on_hChange(QPoint)));
connect(sB_h,SIGNAL(valueChanged(int)),this,SLOT(HSVChange()));
connect(sB_s,SIGNAL(valueChanged(int)),this,SLOT(HSVChange()));
connect(sB_v,SIGNAL(valueChanged(int)),this,SLOT(HSVChange()));
connect(sB_r,SIGNAL(valueChanged(int)),this,SLOT(RGBChange()));
connect(sB_g,SIGNAL(valueChanged(int)),this,SLOT(RGBChange()));
connect(sB_b,SIGNAL(valueChanged(int)),this,SLOT(RGBChange()));
connect(lineText_color,SIGNAL(textChanged(QString)),this,SLOT(textEditChange()));
sB_h->setMaximum(359);sB_s->setMaximum(255);sB_v->setMaximum(255);sB_r->setMaximum(255);sB_g->setMaximum(255);sB_b->setMaximum(255);
pB_selectFromScreen=new QPushButton("从屏幕获取(空格暂停)",this);pB_selectFromScreen->move(320,220);
pB_selectFromScreen->setStyleSheet("QPushButton{background-color:rgb(100,100,100);border:1px solid rgb(60,60,60);border-radius:3px;color:rgb(210,210,210);}");pB_selectFromScreen->setFixedSize(QSize(140,25));
QFont font_selectFromScreen("华康方圆体W7");font_selectFromScreen.setPixelSize(10);
pB_selectFromScreen->setFont(font_selectFromScreen);
connect(pB_selectFromScreen,SIGNAL(clicked()),this,SLOT(pB_clicked()));
m_timer = new QTimer(this);
m_timer->setInterval(10); // 每 100 毫秒检查一次鼠标位置
connect(m_timer, &QTimer::timeout, this, &ColorSelectedFrame::timerStart);
};
~ColorSelectedFrame(){};
//只有这两个命名是反着来的
SVSelectLabel* sv_lbl;
HSelectLabel* h_lbl;
QLabel* lbl_h;QSpinBox* sB_h;
QLabel* lbl_s;QSpinBox* sB_s;
QLabel* lbl_v;QSpinBox* sB_v;
QLabel* lbl_r;QSpinBox* sB_r;
QLabel* lbl_g;QSpinBox* sB_g;
QLabel* lbl_b;QSpinBox* sB_b;
QLabel* lbl_nowColor;
ColorSelectedLineEdit* lineText_color;
QPushButton* pB_selectFromScreen;
QSize size_lbl;
QSize size_sB;
QColor result_rgb;
QColor result_hsv;
QString colorStr;
QPainter painter;
QPen pen_ellipse;
QPen pen_rect;
//QImage tempImg;
QPixmap tempPixmap;
QTimer *m_timer;
QPoint lastMousePosition;
bool isSelectFromScreen=false;
public slots:
void selectFromScreen(QPoint mousePos)
{
QScreen* s=this->screen();
QPixmap pixmap=s->grabWindow(0);
QImage image=pixmap.toImage();
QColor color=image.pixelColor(mousePos);
QPixmap* nowPixmap=new QPixmap(lbl_nowColor->width(),lbl_nowColor->height());nowPixmap->fill(color);
lbl_nowColor->setPixmap(*nowPixmap);
this->lbl_nowColor->setPixmap(*nowPixmap);
qDebug()<<color;
}
void drawEllipse(QPoint mousePos)
{
tempPixmap=sv_lbl->backupPixmap.copy();
painter.begin(&tempPixmap);
painter.setPen(pen_ellipse);
painter.drawEllipse(mousePos,5,5);
this->sv_lbl->setPixmap(tempPixmap);
painter.end();
}
void drawRect(QPoint mousePos)
{
tempPixmap=h_lbl->backupPixmap.copy();
painter.begin(&tempPixmap);
painter.setRenderHint(QPainter::Antialiasing);//抗锯齿
painter.setPen(pen_rect);
painter.drawRect(pen_rect.width()/2,mousePos.y()-10,h_lbl->getWidth()-pen_rect.width(),20);
this->h_lbl->setPixmap(tempPixmap);
painter.end();
}
//========================================各部分改变事件=====================================================
//一共六块部分(sv_lbl,h_lbl,RGB,HSV,lbl_nowColor,lineText_color),任何一块改变都要关联其他部分。一共五块
void on_svChange(QPoint mousePos)//sv_lbl鼠标按下,按下移动的槽函数
{
m_timer->stop();isSelectFromScreen=false;
//blocker这么命名就行,方便检查
//---------------第零块-----sv_lbl-----------------
//lbl的绘制不与其他信号或槽关联,只是一种附带行为,所以不用加QSignalBlocker
drawEllipse(mousePos);
//---------------第一块-----HSV-----------------
QSignalBlocker blocker1(sB_s);
this->sB_s->setValue(mousePos.x());
QSignalBlocker blocker2(sB_v);
this->sB_v->setValue(mousePos.y());
result_hsv=QColor::fromHsv(sB_h->value(),sB_s->value(),sB_v->value());
result_rgb=result_hsv.toRgb();
//---------------第二块-----RGB-----------------
QSignalBlocker blocker3(sB_r);QSignalBlocker blocker4(sB_g);QSignalBlocker blocker5(sB_b);
sB_r->setValue(result_rgb.red());this->sB_g->setValue(result_rgb.green());this->sB_b->setValue(result_rgb.blue());
//---------------第三块-----lbl_nowColor-----------------
QSignalBlocker blocker6(lbl_nowColor);
QPixmap* pixmap=new QPixmap(lbl_nowColor->width(),lbl_nowColor->height());pixmap->fill(result_rgb);
lbl_nowColor->setPixmap(*pixmap);
//---------------第四块-----lineText_color-----------------
QSignalBlocker blocker7(lineText_color);
colorStr=result_rgb.name();
lineText_color->setText(colorStr);
//---------------第五块-----h_lbl-----------------
//不用改
};
void on_hChange(QPoint mousePos)//h_lbl鼠标按下,按下移动的槽函数
{
m_timer->stop();isSelectFromScreen=false;
//---------------第零块-----h_lbl-----------------
drawRect(mousePos);
//---------------第一块-----sv_lbl-----------------
this->sv_lbl->pixmapChange(mousePos.y());//pixmapChange自带从255到360的转变
drawEllipse(QPoint(sB_s->value(),sB_v->value()));
//---------------第二块-----HSV-----------------
QSignalBlocker blocker1(sB_h);
sB_h->setValue((int)((mousePos.y()+1)*1.40625)-1);
result_hsv=QColor::fromHsv(sB_h->value(),sB_s->value(),sB_v->value());
result_rgb=result_hsv.toRgb();
//---------------第三块-----RGB-----------------
QSignalBlocker blocker2(sB_r); QSignalBlocker blocker3(sB_g); QSignalBlocker blocker4(sB_b);
this->sB_r->setValue(result_rgb.red());this->sB_g->setValue(result_rgb.green());this->sB_b->setValue(result_rgb.blue());
//---------------第四块-----lbl_nowColor-----------------
QSignalBlocker blocker5(lbl_nowColor);
QPixmap* pixmap=new QPixmap(lbl_nowColor->width(),lbl_nowColor->height());pixmap->fill(result_rgb);
lbl_nowColor->setPixmap(*pixmap);
//---------------第五块-----lineText_color-----------------
QSignalBlocker blocker6(lineText_color);
colorStr=result_rgb.name();
lineText_color->setText(colorStr);
};
void RGBChange()
{
m_timer->stop();isSelectFromScreen=false;
result_rgb=QColor::fromRgb(sB_r->value(),sB_g->value(),sB_b->value());
result_hsv=result_rgb.toHsv();
//---------------第零块-----RGB-----------------
//---------------第一块-----HSV-----------------
QSignalBlocker blocker1(sB_h);QSignalBlocker blocker2(sB_s);QSignalBlocker blocker3(sB_v);
this->sB_h->setValue(result_hsv.hue());this->sB_s->setValue(result_hsv.saturation());this->sB_v->setValue(result_hsv.value());
//---------------第二块-----sv_lbl-----------------
this->sv_lbl->pixmapChange((int)((sB_h->value()+1)/1.40625)-1);
drawEllipse(QPoint(sB_s->value(),sB_v->value()));
//---------------第三块-----h_lbl-----------------
drawRect(QPoint(0,(int)((sB_h->value()+1)/1.40625)-1));
//---------------第四块-----lbl_nowColor-----------------
change_to_nowColorLabel();
//---------------第五块-----textEdit_color-----------------
change_to_colorTextEdit();
}
void HSVChange()
{
m_timer->stop();isSelectFromScreen=false;
result_hsv=QColor::fromHsv(sB_h->value(),sB_s->value(),sB_v->value());
result_rgb=result_hsv.toRgb();
//---------------第零块-----HSV-----------------
//---------------第一块-----RGB-----------------
QSignalBlocker blocker1(sB_r);QSignalBlocker blocker2(sB_g);QSignalBlocker blocker3(sB_b);
this->sB_r->setValue(result_rgb.red());this->sB_g->setValue(result_rgb.green());this->sB_b->setValue(result_rgb.blue());
//---------------第二块-----sv_lbl-----------------
this->sv_lbl->pixmapChange((int)((sB_h->value()+1)/1.40625)-1);
drawEllipse(QPoint(sB_s->value(),sB_v->value()));
//---------------第三块-----h_lbl-----------------
drawRect(QPoint(0,(int)((sB_h->value()+1)/1.40625)-1));
//---------------第四块-----lbl_nowColor-----------------
change_to_nowColorLabel();
//---------------第五块-----textEdit_color-----------------
change_to_colorTextEdit();
}
void textEditChange()
{
m_timer->stop();isSelectFromScreen=false;
//---------------第零块-----lineText_color-----------------
QString colorText=lineText_color->text();
int r,g,b;
if(colorText.left(1)=="#"){colorText.remove(0, 1);}
QRegularExpression regular("[0-9a-fA-F]{6}");
if(regular.match(colorText).hasMatch())
{
QString temp_r=colorText.mid(0, 2);r=temp_r.toInt(nullptr, 16);
QString temp_g=colorText.mid(2, 2);g=temp_g.toInt(nullptr, 16);
QString temp_b=colorText.mid(4, 2);b=temp_b.toInt(nullptr, 16);
//---------------第一块-----RGB-----------------
QSignalBlocker blocker1(sB_r);QSignalBlocker blocker2(sB_g);QSignalBlocker blocker3(sB_b);
this->sB_r->setValue(r);this->sB_g->setValue(g);this->sB_b->setValue(b);
result_rgb=QColor::fromRgb(r,g,b);
result_hsv=result_rgb.toHsv();
//---------------第二块-----HSV-----------------
QSignalBlocker blocker4(sB_h);QSignalBlocker blocker5(sB_s);QSignalBlocker blocker6(sB_v);
this->sB_h->setValue(result_hsv.hue());this->sB_s->setValue(result_hsv.saturation());this->sB_v->setValue(result_hsv.value());
//---------------第三块-----sv_lbl-----------------
this->sv_lbl->pixmapChange((int)((sB_h->value()+1)/1.40625)-1);
drawEllipse(QPoint(sB_s->value(),sB_v->value()));
//---------------第四块-----h_lbl-----------------
drawRect(QPoint(0,(int)((sB_h->value()+1)/1.40625)-1));
//---------------第五块-----lbl_nowColor-----------------
change_to_nowColorLabel();
}
}
void change_to_nowColorLabel()
{
QPixmap* pixmap=new QPixmap(lbl_nowColor->width(),lbl_nowColor->height());pixmap->fill(result_rgb);
this->lbl_nowColor->setPixmap(*pixmap);
}
void change_to_colorTextEdit()
{
QSignalBlocker blocker1(lineText_color);
colorStr=result_rgb.name();
lineText_color->setText(colorStr);
}
void pB_clicked()
{
if(!isSelectFromScreen)
{
m_timer->start();isSelectFromScreen=true;
}
else{m_timer->stop();isSelectFromScreen=false;}
}
void timerStart()
{
QPoint currentMousePosition = QCursor::pos();
if (currentMousePosition != lastMousePosition) {
lastMousePosition = currentMousePosition;
//QMouseEvent *event = new QMouseEvent(QEvent::MouseMove, currentMousePosition, Qt::NoButton, Qt::NoButton, Qt::NoModifier);
QPixmap pixmap = screen()->grabWindow(0, lastMousePosition.x(), lastMousePosition.y(), 1, 1);
QImage image=pixmap.toImage();
//QBitmap bitmap = pixmap1.toBitmap();
QColor pixelColor = image.pixel(0, 0);
//QColor color = QColor(pixelColor);
//QPixmap* nowPixmap=new QPixmap(lbl_nowColor->width(),lbl_nowColor->height());nowPixmap->fill(pixelColor);
//lbl_nowColor->setPixmap(*nowPixmap);
//qApp->sendEvent(this, event);
result_rgb=pixelColor;
result_hsv=result_rgb.toHsv();
//---------------第一块-----RGB-----------------
QSignalBlocker blocker1(sB_r);QSignalBlocker blocker2(sB_g);QSignalBlocker blocker3(sB_b);
this->sB_r->setValue(result_rgb.red());this->sB_g->setValue(result_rgb.green());this->sB_b->setValue(result_rgb.blue());
//---------------第二块-----HSV-----------------
QSignalBlocker blocker4(sB_h);QSignalBlocker blocker5(sB_s);QSignalBlocker blocker6(sB_v);
this->sB_h->setValue(result_hsv.hue());this->sB_s->setValue(result_hsv.saturation());this->sB_v->setValue(result_hsv.value());
//---------------第三块-----sv_lbl考虑到目的没必要设置-----------------
//---------------第三块-----h_lbl考虑到目的没必要设置-----------------
//---------------第五块-----lbl_nowColor-----------------
change_to_nowColorLabel();
//---------------第六块-----lineText_color-----------------
change_to_colorTextEdit();
}
}
protected:
void mousePressEvent(QMouseEvent *event) override
{
m_timer->stop();isSelectFromScreen=false;
};
void keyPressEvent(QKeyEvent *event) override
{
if(event->key()==QChar::Space)
{
m_timer->stop();isSelectFromScreen=false;
}
};
};
然后ColorSelectedFrame frame=new ColorSelectedFrame(this);
就能按QFrame自由使用了
这东西挺简单的,但是分享的人貌似比较少
介绍
大体这样实现:
不算那个“从屏幕取色”。其实就是定义六块部分:
1、一个QLabel用于显示渐变色板(S,V通道)
2、一个QLabel用于显示渐变色板(H通道)
3、三个QSpinBox用于显示H,S,V的数值
4、三个QSpinBox用于显示R,G,B数值
5、一个QLbel用于显示当前选色
6、一个QTextLine用于显示色彩名称“#......”
然后任何一个区块改变,都直接连接槽让其他五个区块也改变,这些都由在ColorSelectedFrame里定义,由它统筹完成;用QSignalBlocker防止他们无限地相互更改。
“从屏幕取色”功能不需要用事件过滤器,用QTimer每隔10ms获取一下当前鼠标位置。再获取屏幕上这个位置的颜色,有了颜色一切都好办,赋值给result_rgb然后使其他六个区块也改变。
注意
1、关于命名规范,比如sB_h,“sB”是“QSpinBox”的缩写,这些大家应该都知道,“h”就是hsv中的h通道。除了(sv_lbl和h_lbl这两个怕重名,所以没按照命名规范)
2、这里的几个QSpinBox非常重要,因为颜色数据大部分都是获取QSpinBox的值
3、样式表和布局设置是在ColorSelectedFrame的构造函数里完成的,大家可以自行更改
4、此程序已确保可成功运行。如果需要优化请便。注意ColorSelectedFrame里定义的变量,注意信号槽