0
点赞
收藏
分享

微信扫一扫

Qt开发高级进阶: 用lambda表达式写一个跨平台的屏幕取色器

经常要屏幕取色,以前网上下的工具找不到了,就随手写了一个。

Qt开发高级进阶: 用lambda表达式写一个跨平台的屏幕取色器_desktop

源码地址:

​​https://github.com/SpaceView/screenColorPicker_QtQt开发高级进阶: 用lambda表达式写一个跨平台的屏幕取色器_desktop_02https://github.com/SpaceView/screenColorPicker_Qt​​这里已经有编译好的screenColorPicker.exe在bin文件夹内,下载即可使用。

库是Qt5.15.0,直接到网上下载找到那几个库就行了。我已经上传了Qt5Widgets.dll,Qt5GUI.dll, Qt5Core.dll,应该可以直接跑起来了。Ubuntu下的话就自己编译一下吧。

源码非常简单,只有短短十多行。

timer = new QTimer(this);
timer->start(400);
image = new QImage;
pm = new QPixmap;
setWindowTitle("Screen Color Picker");

connect(timer,&QTimer::timeout,this, [this](){
this->pos = QCursor::pos();
qDebug()<< "current screen pos = " << this->pos;

QScreen *screen = QGuiApplication::primaryScreen();
//*pm = QPixmap::grabWindow(QApplication::desktop()->winId());
*pm = screen->grabWindow(QApplication::desktop()->winId());
*image = pm->toImage();
QRgb b = image->pixel(this->pos);
//QColor *c = new QColor;
color.setRgb(b);
QColor ic = QColor(255- color.red(), 255-color.green(), 255-color.blue());
//qDebug() << c->name();
ui->lineEdit->setText(color.name());
QString str = "QLabel { background-color :" + color.name() + " ; color :" + ic.name() + " ; }";
ui->label->setStyleSheet(str);

说明:这里主要就是利用lambda表达式在时钟timeout来临时,读取一次屏幕当前像素的颜色。

其中时间长度设为400ms读一次。需要的话可更设置得更小一些,以消除滞后的感觉。

本文结束。


举报

相关推荐

0 条评论