当前位置:网站首页>Color finder actual combat (QT including source code)
Color finder actual combat (QT including source code)
2022-07-28 21:02:00 【Sanlei Technology】
Color finder is a small tool commonly used in software development , For convenience in development , I developed a color finder myself , Simple to use , Mainly, if there are defects in the function, I can add it myself .
One 、 Function is introduced
1. Click to choose to automatically capture the current screen image , Get the color of the current corresponding position by selecting the pigment of the current picture .
2. Copy button , You can copy the contents of the selection box on the right .
3. By moving the bar box , You can choose the corresponding color RGB value .
4. By input RGB Value can be obtained directly on the right 16 The value of base .
5. By input 6 position 16 Hexadecimal value can obtain the corresponding RGB value .
Two 、 Function development
1. Layout
The layout description is shown in the figure below :
main The layout uses a vertical layout . And then every one layout Use horizontal layout .
void MainWindow::initUI()
{
setWindowTitle(" Color picker ");
m_ptrWdgMain->setLayout(m_ptrLayoutMain);
m_ptrLabShowColor = updateShowLable(m_ptrLabShowColor, Qt::black);
m_ptrLabShowColor->setFixedSize(50, 50);
m_ptrLayoutShowColor->addWidget(m_ptrLabShowColor);
m_ptrLabShowRed = updateShowLable(m_ptrLabShowRed, Qt::black);
m_ptrSliderRed = updateSlider(m_ptrSliderRed);
m_ptrLabRed = updateShowLable(m_ptrLabRed, Qt::red);
m_ptrLayoutRed->addWidget(m_ptrLabShowRed);
m_ptrLayoutRed->addWidget(m_ptrSliderRed);
m_ptrLayoutRed->addWidget(m_ptrLabRed);
m_ptrLayoutRed->addWidget(m_ptrLineRed);
m_ptrLineRed->setPlaceholderText("0-255");
m_ptrLineRed->setText("0");
m_ptrLabShowGreen = updateShowLable(m_ptrLabShowGreen, Qt::black);
m_ptrSliderGreen = updateSlider(m_ptrSliderGreen);
m_ptrLabGreen = updateShowLable(m_ptrLabGreen, Qt::green);
m_ptrLayoutGreen->addWidget(m_ptrLabShowGreen);
m_ptrLayoutGreen->addWidget(m_ptrSliderGreen);
m_ptrLayoutGreen->addWidget(m_ptrLabGreen);
m_ptrLayoutGreen->addWidget(m_ptrLineGreen);
m_ptrLineGreen->setPlaceholderText("0-255");
m_ptrLineGreen->setText("0");
m_ptrLabShowBlue = updateShowLable(m_ptrLabShowBlue, Qt::black);
m_ptrSliderBlue = updateSlider(m_ptrSliderBlue);
m_ptrLabBlue = updateShowLable(m_ptrLabBlue, Qt::blue);
m_ptrLayoutBlue->addWidget(m_ptrLabShowBlue);
m_ptrLayoutBlue->addWidget(m_ptrSliderBlue);
m_ptrLayoutBlue->addWidget(m_ptrLabBlue);
m_ptrLayoutBlue->addWidget(m_ptrLineBlue);
m_ptrLineBlue->setPlaceholderText("0-255");
m_ptrLineBlue->setText("0");
m_ptrLayoutButton->addWidget(m_ptrBtnPickColor);
m_ptrLayoutButton->addWidget(m_ptrBtnCopy);
m_ptrLayoutButton->addWidget(m_ptrLineColor);
m_ptrLineColor->setPlaceholderText("6 position 16 Base number ");
m_ptrLayoutMain->addLayout(m_ptrLayoutShowColor);
m_ptrLayoutMain->addLayout(m_ptrLayoutRed);
m_ptrLayoutMain->addLayout(m_ptrLayoutGreen);
m_ptrLayoutMain->addLayout(m_ptrLayoutBlue);
m_ptrLayoutMain->addLayout(m_ptrLayoutButton);
this->setCentralWidget(m_ptrWdgMain);
}
2. Control message binding
QAbstractSlider::valueChanged : Triggered when the value of the slide button changes .
QLineEdit::textChanged: Triggered when the Edit button changes .
QLineEdit::returnPressed: Triggered when the result edit box returns .
QPushButton::clicked : Triggered when the button is pressed .
void MainWindow::initConnect()
{
connect(m_ptrSliderRed, SIGNAL(valueChanged(int)), this,
SLOT(slotRedValue(int)));
connect(m_ptrSliderGreen, SIGNAL(valueChanged(int)), this,
SLOT(slotGreenValue(int)));
connect(m_ptrSliderBlue, SIGNAL(valueChanged(int)), this,
SLOT(slotBlueValue(int)));
connect(m_ptrBtnPickColor, SIGNAL(clicked()), this, SLOT(slotPickColor()));
connect(m_ptrBtnCopy, SIGNAL(clicked()), this, SLOT(slotCopyColorValue()));
connect(m_ptrWdgPick, SIGNAL(finished(int)), this,
SLOT(slotClosePickWidget(int)));
connect(m_ptrWdgPick, &QPickWidget::sigColor, this,
&MainWindow::slotGetColor);
connect(m_ptrLineRed, SIGNAL(textChanged(QString)), this,
SLOT(slotRedValueChange(QString)));
connect(m_ptrLineGreen, SIGNAL(textChanged(QString)), this,
SLOT(slotGreenValueChange(QString)));
connect(m_ptrLineBlue, SIGNAL(textChanged(QString)), this,
SLOT(slotBlueValueChange(QString)));
// Bind carriage return
connect(m_ptrLineColor, &QLineEdit::returnPressed, this, [this]() {
QString strColor = m_ptrLineColor->text();
if (strColor.size() != 6) {
return;
}
QColor color("#" + strColor);
m_ptrLineRed->setText(QString::number(color.red()));
m_ptrLineGreen->setText(QString::number(color.green()));
m_ptrLineBlue->setText(QString::number(color.blue()));
qInfo() << "m_ptrLineColor" << m_ptrLineColor->text();
});
}
3. Control message processing
3.1 Button select button
Hide this app and , Then capture the full screen of the desktop , Then show the application pictures on the full screen .
void MainWindow::slotPickColor()
{
this->hide();
QTimer::singleShot(200, this, &MainWindow::slotShowPick);
}
void MainWindow::slotShowPick()
{
QScreen *screen = QGuiApplication::primaryScreen();
m_ptrWdgPick->setPickPicture(screen->grabWindow(0));
m_ptrWdgPick->showFullScreen();
}
3.2 Scroll through the selection box
When the current scroll value changes , Change the content of the text edit box .
void MainWindow::slotGreenValueChange(QString strValue)
{
m_ptrSliderGreen->setValue(strValue.toInt());
}
3.3 The value of the input box changes
When the user enters content in the input box , Modify the value of the scroll bar , And change the color .
void MainWindow::updateShowColor()
{
m_ptrLabShowColor->setPalette(
QPalette(QPalette::Background, QColor(m_iRed, m_iGreen, m_iBulle)));
QString strRed = QString("%1").arg(m_iRed, 2, 16, QLatin1Char('0'));
QString strGreen = QString("%1").arg(m_iGreen, 2, 16, QLatin1Char('0'));
QString strBlue = QString("%1").arg(m_iBulle, 2, 16, QLatin1Char('0'));
m_ptrLineColor->setText(strRed + strGreen + strBlue);
}
void MainWindow::slotRedValue(int value)
{
m_ptrLineRed->setText(QString::number(value));
m_iRed = value;
updateLable(value, Red);
updateShowColor();
}
4. Selection box logic
Get the coordinates of the mouse .
1. Use controls qlabel To show the enlarged picture .
2. Mouse movement qlabel Control moves with .
3. Intercept the picture of the mouse position point .
#include "qpickwidget.h"
#include <QKeyEvent>
#include <QtDebug>
#include <QBrush>
#include <QPalette>
#include <QRgb>
QPickWidget::QPickWidget(QWidget *parent)
: QDialog(parent)
, m_ptrLabMouse(new QLabel(this))
{
m_ptrLabMouse->setFixedSize(100, 100);
m_ptrLabMouse->setAutoFillBackground(true);
m_ptrLabMouse->setPalette(QPalette(QPalette::Background, QColor(Qt::red)));
m_ptrLabMouse->setAttribute(Qt::WA_TransparentForMouseEvents, true);
this->setMouseTracking(true);
setCursor(Qt::CrossCursor);
initConnect();
}
void QPickWidget::setPickPicture(QPixmap pixmap)
{
m_pixmapPickPicture = pixmap;
QPalette p;
p.setBrush(QPalette::Background, QBrush(pixmap));
this->setPalette(p);
}
void QPickWidget::initConnect()
{
// connect(this, SIGNAL(clicked()), this, SLOT(slotPickColor));
}
void QPickWidget::slotPickColor() { this->close(); }
void QPickWidget::keyPressEvent(QKeyEvent *event)
{
if (event->key() == Qt::Key_Escape) {
this->close();
}
}
void QPickWidget::showEvent(QShowEvent *event) { qInfo() << "showEvent"; }
void QPickWidget::mouseMoveEvent(QMouseEvent *event)
{
int mouse_x = event->x();
int mouse_y = event->y();
m_ptrLabMouse->move(mouse_x - m_ptrLabMouse->width() / 2,
mouse_y - m_ptrLabMouse->height() / 2);
// QRect rect(mouse_x - 3.5, mouse_y, 7, 7);
QPixmap cropped =
m_pixmapPickPicture.copy(mouse_x -4, mouse_y -4, 9, 9);
QPixmap cropped1 = cropped.scaled(100, 100, Qt::KeepAspectRatio);
QPalette p;
p.setBrush(QPalette::Background, QBrush(cropped1));
m_ptrLabMouse->setPalette(p);
}
void QPickWidget::mousePressEvent(QMouseEvent *event)
{
qInfo() << "mousePressEvent";
QRgb tmp_rgb = m_pixmapPickPicture.toImage().pixel(event->x(), event->y());
QColor rgb(tmp_rgb);
emit sigColor(rgb.red(), rgb.green(), rgb.blue());
this->close();
}
5. Source code
Source code address : Sanlei Technology / getColor · GitCode
边栏推荐
- dll反编译(反编译加密dll)
- Unity foundation 5-optimization strategy
- Unity3d tutorial notes - unity initial 02
- Want to draw a picture that belongs to you? AI painting, you can also
- Space shooting Lesson 15: props
- Oracle library access is slow. Why?
- Read the recent trends of okaleido tiger and tap the value and potential behind it
- Integrating database Ecology: using eventbridge to build CDC applications
- 融合数据库生态:利用 EventBridge 构建 CDC 应用
- 研发效能的思考总结
猜你喜欢
Redis 3.0 source code analysis - data structure and object SDS list Dict
Cause analysis of restart of EMC cx4-120 SPB controller
Subcontracting loading of wechat applet
Seventeen year operation and maintenance veterans, ten thousand words long, speak through the code of excellent maintenance and low cost~
Pl515 SOT23-5 single / Dual Port USB charging protocol port controller Parkson electronic agent
Integrating database Ecology: using eventbridge to build CDC applications
Explain in detail the rays and radiographic testing in unity
Introduction to redis I: redis practical reading notes
MoCo V1:视觉领域也能自监督啦
【服务器数据恢复】HP StorageWorks系列存储RAID5两块盘故障离线的数据恢复案例
随机推荐
瀚高数据库最佳实践配置工具HG_BP日志采集内容
mysql还有哪些自带的函数呢?别到处找了,看这个就够了。
第六七八次作业
Introduction to redis I: redis practical reading notes
The cloud native programming challenge is hot, with 510000 bonus waiting for you to challenge!
全链路灰度在数据库上我们是怎么做的?
3D laser slam: Interpretation of logo-loam paper - Introduction
Unity foundation 6-rotation
MySQL修改端口号(修改mysql的端口号会有问题吗)
Unity foundation 5-optimization strategy
The 678th operation
C # basic 4-written examination question 1
Random talk on GIS data (VI) - projection coordinate system
[工具类] Map的util包, 常用 实体类转化为map等操作
PL515 SOT23-5 单/双口 USB 充电协议端口控制器 百盛电子代理商
MoCo V2:MoCo系列再升级
JS picture hanging style photo wall JS special effect
Unity foundation 1 - event execution sequence, custom events
到底为什么不建议使用SELECT * ?
又一款装机神器