Qt 小项目 -- 颜色拾取器

颜色拾取器


参考 liudianwu 大神的作品: http://www.qtcn.org/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_OBJECT
public:
    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;
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值