颜色拾取器
文章目录
颜色拾取器项目文件.promain.cppWidget.hWidget.cpp参考 liudianwu 大神的作品: /bbs/read-htm-tid-46711-ds-1.html#tpc
项目文件.pro
// 添加项目图标RC_ICONS = 1.ico
main.cpp
#include "Widget.h"#include <QApplication>int main(int argc, char *argv[]){QApplication a(argc, argv);Widget w;w.show();return a.exec();}
Widget.h
#ifndef WIDGET_H#define WIDGET_H#include <QtWidgets>class Widget : public QWidget{Q_OBJECTpublic:Widget(QWidget *parent = nullptr);public slots:void showColorValue();void mouseDoubleClickEvent(QMouseEvent *event) override;void changeEvent(QEvent *) override;private:QWidget *render; //显示区域QLabel *webLabel;QLabel *rgbLabel;QLabel *corLabel;QLineEdit *webEdit;QLineEdit *rgbEdit;QLineEdit *corEdit;bool shoot = false;};#endif // WIDGET_H
Widget.cpp
#include "Widget.h"Widget::Widget(QWidget *parent): QWidget(parent){render = new QWidget;render->setSizePolicy(QSizePolicy::Fixed,QSizePolicy::Fixed);render->setMinimumSize(80,80);render->setAutoFillBackground(true);//调色板QPalette palette ;palette.setColor(QPalette::Base,Qt::black); //QLineEdit背景palette.setColor(QPalette::Text,QColor(238,188,30));//QLineEdit前景palette.setColor(QPalette::Window,QColor(238,188,30));//QWidget使用Window角色render->setPalette(palette);webLabel = new QLabel("Web值:");webLabel->setAlignment(Qt::AlignRight);webEdit = new QLineEdit;webEdit->setPalette(palette);webEdit->setReadOnly(true); //只读rgbLabel = new QLabel("RGB值:");rgbLabel->setAlignment(Qt::AlignRight);rgbEdit = new QLineEdit;rgbEdit->setPalette(palette);rgbEdit->setReadOnly(true);corLabel = new QLabel("坐标值:");corLabel->setAlignment(Qt::AlignRight);corEdit = new QLineEdit;corEdit->setPalette(palette);corEdit->setReadOnly(true);QGridLayout *mainLayout = new QGridLayout(this);mainLayout->addWidget(render,0,0,3,1);mainLayout->addWidget(webLabel,0,1);mainLayout->addWidget(webEdit,0,2);mainLayout->addWidget(rgbLabel,1,1);mainLayout->addWidget(rgbEdit,1,2);mainLayout->addWidget(corLabel,2,1);mainLayout->addWidget(corEdit,2,2);setMinimumSize(344,112);setMaximumSize(344,112);setWindowTitle("颜色拾取器");setWindowFlags(Qt::WindowStaysOnTopHint);}void Widget::showColorValue(){int x = QCursor::pos().x();int y = QCursor::pos().y();corEdit->setText(QString("X:%1 Y:%2").arg(x,5).arg(y,5));QString strDecimalValues,strHex;QScreen *screen = QApplication::primaryScreen();QPixmap pixmap = screen->grabWindow(QApplication::desktop()->winId(),x,y,2,2);if(!pixmap.isNull()) {QImage image = pixmap.toImage();if(!image.isNull()) {if(image.valid(0,0)) {QColor color = image.pixel(0,0);int red= color.red();int green = color.green();int blue = color.blue();QString strRed= QString("%1").arg(red&0xFF,2,16,QLatin1Char('0'));QString strGreen = QString("%1").arg(green&0xFF,2,16,QLatin1Char('0'));QString strBlue= QString("%1").arg(blue&0xFF,2,16,QLatin1Char('0'));strDecimalValues = QString("%1,%2,%3").arg(red).arg(green).arg(blue);strHex = QString("#%1%2%3").arg(strRed.toUpper()).arg(strGreen.toUpper()).arg(strBlue.toUpper());}}}QString str = QString("background-color: rgb(%1);").arg(strDecimalValues);render->setStyleSheet(str);webEdit->setText(strHex);rgbEdit->setText(strDecimalValues);if(shoot)QTimer::singleShot(100,this,&Widget::showColorValue);}void Widget::mouseDoubleClickEvent(QMouseEvent *){shoot = true;showColorValue();}void Widget::changeEvent(QEvent *){shoot = false;}