当前位置: 首页 > news >正文

Qt QML实现Windows桌面颜色提取器

前言

实现一个简单的小工具,使用Qt QML实现Windows桌面颜色提取器,实时显示鼠标移动位置的颜色值,包括十六进制值和RGB值。该功能在实际应用中比较常见,比如截图的时候,鼠标移动就会在鼠标位置实时显示坐标和颜色值,做项目实现UI的时候可能会经常用到这个功能,快速提取设计图的颜色值。

效果图如下:
在这里插入图片描述
代码使用Qt 5.15.2版本实现,QML实现UI交互。
后期可以在此基础上扩展更多的功能,比如鼠标右键的时候,就暂停提取,然后添加一个复制按钮,可以方便复制当前的颜色值,然后直接取用。

正文

实现原理就是移动移动的时候,实时在屏幕鼠标处截取图片然后获取图片上的颜色值。

    QPixmap pixmap = screen->grabWindow(0, screenPos.x(), screenPos.y(), 1, 1);QImage image = pixmap.toImage();if (image.isNull() || image.width() < 1 || image.height() < 1) {return; }QColor color = QColor(image.pixel(0, 0));
Rectangle {anchors.fill: parentcolor: "#f0f0f0"border.color: "#cccccc"border.width: 1radius: 5ColumnLayout {anchors.fill: parentanchors.margins: 10spacing: 10Text {Layout.alignment: Qt.AlignHCentertext: "颜色提取器"font.pixelSize: 18font.bold: true}Rectangle {Layout.fillWidth: trueLayout.preferredHeight: 50color: colorPicker.isActive ? colorPicker.hexColor : "#cccccc"border.color: "#999999"border.width: 1radius: 4Text {anchors.centerIn: parenttext: colorPicker.isActive ? colorPicker.hexColor : "等待提取颜色..."color: colorPicker.isActive ? (isDarkColor(colorPicker.red, colorPicker.green, colorPicker.blue) ? "white" : "black") : "black"font.pixelSize: 14}}Text {Layout.alignment: Qt.AlignHCentertext: colorPicker.isActive ? colorPicker.rgbColor : "RGB(0, 0, 0)"font.pixelSize: 14}Button {Layout.fillWidth: trueLayout.preferredHeight: 40text: colorPicker.isActive ? "停止提取" : "开始提取"onClicked: {if (colorPicker.isActive) {colorPicker.stopPicking()} else {colorPicker.startPicking()}}}Button {Layout.fillWidth: trueLayout.preferredHeight: 30text: "退出"onClicked: {Qt.quit()}}}}

完整Demo下载

相关文章:

  • JVM:类加载子系统
  • android rtsp 拉流h264 h265,解码nv12转码nv21耗时卡顿问题及ffmpeg优化
  • 基于多模态深度学习的亚急性脊髓联合变性全流程预测与个性化管理技术方案
  • JVM:对象的实例化、直接内存
  • cfd笔记【1】简介
  • vue js 上传文件 form data
  • 一个含有 n 个顶点的连通且无环的简单无向图,在其邻接矩阵存储结构中共有几个零元素?
  • Hadoop集群部署教程-END
  • 【25软考网工笔记】第二章 数据通信基础(4)数据编码
  • Flask快速入门
  • Flask(1): 在windows系统上部署项目1
  • android11通过白名单卸载安装应用
  • NET 注入日志Logger错误
  • Rust : 关于*const () 与type erase
  • 算法-同余原理
  • 《MySQL下载安装:Linux下载MySQL 5.7版本》
  • 镜舟科技助力某大型电网企业破解数据架构升级难题,打造国产化湖仓标杆
  • 每天学一个 Linux 命令(18):mv
  • Qt实现文件传输服务器端(图文详解+代码详细注释)
  • tar压缩
  • 青岛网站建设哪家专业/专业的seo搜索引擎优化培训
  • 做网站解析要多久/营销技巧美剧
  • b2c网站建设方案书/百度推广渠道户
  • 网站设计的简称/网站查询域名入口
  • 政府网站集约化建设专题/市场调研的基本流程
  • wordpress链接自动加斜杠/seo顾问服务