《QT实用小工具·四》屏幕拾色器

1、概述
源码放在文章末尾

该项目实现了屏幕拾色器的功能,可以根据鼠标指定的位置识别当前位置的颜色

项目功能包含:
鼠标按下实时采集鼠标处的颜色。
实时显示颜色值。
支持16进制格式和rgb格式。
实时显示预览颜色。
根据背景色自动计算合适的前景色。

下面是demo演示:
在这里插入图片描述
项目部分代码如下所示:

#pragma execution_character_set("utf-8")#include "colorwidget.h"
#include "qmutex.h"
#include "qgridlayout.h"
#include "qlabel.h"
#include "qlineedit.h"
#include "qapplication.h"
#include "qtimer.h"
#include "qevent.h"
#include "qdebug.h"#if (QT_VERSION >= QT_VERSION_CHECK(5,0,0))
#include "qscreen.h"
#define deskGeometry qApp->primaryScreen()->geometry()
#define deskGeometry2 qApp->primaryScreen()->availableGeometry()
#else
#include "qdesktopwidget.h"
#define deskGeometry qApp->desktop()->geometry()
#define deskGeometry2 qApp->desktop()->availableGeometry()
#endifColorWidget *ColorWidget::instance = 0;
ColorWidget *ColorWidget::Instance()
{if (!instance) {static QMutex mutex;QMutexLocker locker(&mutex);if (!instance) {instance = new ColorWidget;}}return instance;
}ColorWidget::ColorWidget(QWidget *parent) : QWidget(parent)
{gridLayout = new QGridLayout(this);gridLayout->setSpacing(6);gridLayout->setContentsMargins(11, 11, 11, 11);verticalLayout = new QVBoxLayout();verticalLayout->setSpacing(0);labColor = new QLabel(this);labColor->setText("+");labColor->setStyleSheet("background-color: rgb(255, 107, 107);color: rgb(250, 250, 250);");labColor->setAlignment(Qt::AlignCenter);QFont font;font.setPixelSize(35);font.setBold(true);labColor->setFont(font);QSizePolicy sizePolicy(QSizePolicy::Preferred, QSizePolicy::Expanding);sizePolicy.setHorizontalStretch(0);sizePolicy.setVerticalStretch(0);sizePolicy.setHeightForWidth(labColor->sizePolicy().hasHeightForWidth());labColor->setSizePolicy(sizePolicy);labColor->setMinimumSize(QSize(80, 70));labColor->setMaximumSize(QSize(80, 70));labColor->setCursor(QCursor(Qt::CrossCursor));labColor->setFrameShape(QFrame::StyledPanel);labColor->setFrameShadow(QFrame::Sunken);verticalLayout->addWidget(labColor);label = new QLabel(this);label->setMinimumSize(QSize(0, 18));label->setStyleSheet("background-color: rgb(0, 0, 0);color: rgb(200, 200, 200);");label->setAlignment(Qt::AlignCenter);verticalLayout->addWidget(label);gridLayout->addLayout(verticalLayout, 0, 0, 3, 1);labWeb = new QLabel(this);gridLayout->addWidget(labWeb, 0, 1, 1, 1);txtWeb = new QLineEdit(this);gridLayout->addWidget(txtWeb, 0, 2, 1, 1);labRgb = new QLabel(this);gridLayout->addWidget(labRgb, 1, 1, 1, 1);txtRgb = new QLineEdit(this);gridLayout->addWidget(txtRgb, 1, 2, 1, 1);labPoint = new QLabel(this);gridLayout->addWidget(labPoint, 2, 1, 1, 1);txtPoint = new QLineEdit(this);gridLayout->addWidget(txtPoint, 2, 2, 1, 1);label->setText("当前颜色");labWeb->setText("web值:");labRgb->setText("rgb值:");labPoint->setText("坐标值:");this->setLayout(gridLayout);this->setWindowTitle("屏幕拾色器");this->setFixedSize(300, 108);cp = QApplication::clipboard();pressed = false;timer = new QTimer(this);timer->setInterval(100);connect(timer, SIGNAL(timeout()), this, SLOT(showColorValue()));timer->start();
}ColorWidget::~ColorWidget()
{
}void ColorWidget::mousePressEvent(QMouseEvent *e)
{if (labColor->rect().contains(e->pos())) {pressed = true;}
}void ColorWidget::mouseReleaseEvent(QMouseEvent *)
{pressed = false;
}void ColorWidget::showColorValue()
{if (!pressed) {return;}int x = QCursor::pos().x();int y = QCursor::pos().y();txtPoint->setText(tr("x:%1  y:%2").arg(x).arg(y));#if (QT_VERSION >= QT_VERSION_CHECK(5,0,0))QScreen *screen = qApp->primaryScreen();QPixmap pixmap = screen->grabWindow(0, x, y, 2, 2);
#elseQPixmap pixmap = QPixmap::grabWindow(qApp->desktop()->winId(), x, y, 2, 2);
#endifint red, green, blue;QString strDecimalValue, strHex;if (pixmap.isNull()) {return;}QImage image = pixmap.toImage();if (image.valid(0, 0)) {QColor color = image.pixel(0, 0);red = color.red();green = color.green();blue = color.blue();QString strRed = tr("%1").arg(red & 0xFF, 2, 16, QChar('0'));QString strGreen = tr("%1").arg(green & 0xFF, 2, 16, QChar('0'));QString strBlue = tr("%1").arg(blue & 0xFF, 2, 16, QChar('0'));strDecimalValue = tr("%1, %2, %3").arg(red).arg(green).arg(blue);strHex = tr("#%1%2%3").arg(strRed.toUpper()).arg(strGreen.toUpper()).arg(strBlue.toUpper());}//根据背景色自动计算合适的前景色QColor color(red, green, blue);double gray = (0.299 * color.red() + 0.587 * color.green() + 0.114 * color.blue()) / 255;QColor textColor = gray > 0.5 ? Qt::black : Qt::white;QString str = tr("background:rgb(%1);color:%2").arg(strDecimalValue).arg(textColor.name());labColor->setStyleSheet(str);txtRgb->setText(strDecimalValue);txtWeb->setText(strHex);
}

源码下载

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/781111.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

AIGC重塑金融:AI大模型驱动的金融变革与实践

🌈个人主页: Aileen_0v0 🔥热门专栏: 华为鸿蒙系统学习|计算机网络|数据结构与算法 ​💫个人格言:“没有罗马,那就自己创造罗马~” #mermaid-svg-tVrfBkGvUD0Qi13F {font-family:"trebuchet ms",verdana,arial,sans-serif;font-siz…

学习java第二十七天

Spring框架作为IOC容器的落地实现,提供了一个灵活的"插座",其他组件只需要简单的"插上"即可享受Spring提供的基础设施支持- ,并且结合Spring一起使用。 Spring的核心在于它的IOC容器设计,我们可以通过Spring应用程序上下文生命周期和Spring Bean的生命周期…

基于SpringBoot的“游戏分享网站”的设计与实现(源码+数据库+文档+PPT)

基于SpringBoot的“游戏分享网站”的设计与实现(源码数据库文档PPT) 开发语言:Java 数据库:MySQL 技术:SpringBoot 工具:IDEA/Ecilpse、Navicat、Maven 系统展示 系统总体结构图 网站首页界面图 用户注册界面图 …

为什么用了索引,搜索速度还是很慢

索引列选择不当 1.假设有一个包含性别信息的表,其中有1000条记录,其中男性占 99%、女性占 1%。如果在性别列上创建索引,由于选择性太低,大部分查询都会涉及到表中的绝大多数行,这时候数据库优化器可能会认为全表扫描比…

[SpringCloud] Feign Client 的创建 (二) (五)

文章目录 1.自动配置FeignAutoConfiguration2.生成 Feign Client2.1 从Feign Client子容器获取组件2.2 Feign Client子容器的创建2.3 构建Feign Client实例 1.自动配置FeignAutoConfiguration spring-cloud-starter-openfeign 包含了 spring-cloud-openfeign-core FeignAutoCo…

HarmonyOS实战开发-如何实现一个支持加减乘除混合运算的计算器。

介绍 本篇Codelab基于基础组件、容器组件,实现一个支持加减乘除混合运算的计算器。 说明: 由于数字都是双精度浮点数,在计算机中是二进制存储数据的,因此小数和非安全整数(超过整数的安全范围[-Math.pow(2, 53)&#…

【详解】运算放大器工作原理及其在信号处理中的核心作用

什么是运算放大器 运算放大器(简称“运放”)是一种放大倍数非常高的电路单元。在实际电路中,它常常与反馈网络一起组成一定的功能模块。它是一种带有特殊耦合电路和反馈的放大器。输出信号可以是输入信号的加法、减法、微分和积分等数学运算…

vue3+ts项目 | axios 的测试 | 测试接口

在 App.vue 中,测试接口 // 测试接口import request from /utils/request;import { onMounted } from vue;onMounted(() > {request.get(/hosp/hospital/1/10).then((res) > {console.log("APP组件展示获取的数据",res);})}) 在request.ts中&…

link 样式表是否会阻塞页面内容的展示?取决于浏览器,edge 和 chrome 会,但 firefox 不会。

经过实测: 在 head 中 link 一个 1M 大小的样式表。设置网络下载时间大概为 10 秒。 edge 和 chrome 只有在下载完样式表后,页面上才会出现内容。而 firefox 可以直接先显示内容,然后等待样式表下载完成后再应用样式。 DOMContentLoaded 事…

Vue指令之v-for

跟其他语言中的for一样,是用来渲染多个类似实例的。 语法为v-for"(item, index) in 可迭代对象",一般就用于遍历数组。这里的语法跟Python中的for循环enumerate也有点相似之处,但要注意item在前,index在后,…

探索Java学习的精华:必备资料分享

239程序员职业规划手册 238手把手带你写一个MiniTomcat 237Rust语言从入门到实战 236超级访谈:对话道哥 235LangChain实战课 234云时代的JVM原理与实战 233AI大模型系统实战 231AI绘画核心技术与实战【未完整】 Java核心技术面试精讲 从0开始学架构 推荐系统三十六式…

CommandLineRunner解释学习

目录 一、CommandLineRunner介绍 1.1 接口定义 1.2 工作原理 二、CommandLineRunner作用 三、CommandLineRunner使用方式 3.1 实现CommandLineRunner 3.2 配置Spring Boot项目 四、完整代码地址 小剧场:坚持不懈! 一、CommandLineRunner介绍 Co…

anaconda配置虚拟python环境

使用conda create命令 举例: 创建一个名为breed的新环境,并在其中安装python 3.7版本的步骤: 创建虚拟环境 conda create --name breed python3.7激活新创建的环境: conda activate breed查看存在的虚拟环境 conda env list退…

复杂度3 二分查找函数

文章预览&#xff1a; 题目算法代码 题目 算法 本题要求用二分法查找顺序表的一个值&#xff0c;比较简单注意指针格式即可 代码 Position BinarySearch( List L, ElementType X ) {int begin1,mid;int endL->Last;ElementType temp;while(begin<end){mid(beginend)/2…

【Spring Security】 快速入门

文章目录 一、 身份认证Demo1、创建工程2、代码编写2.1、Controller2.2、Html2.3、application.properties配置 3、启动项目并访问 二、Spring Security 默认做了什么二、底层原理1.概述2.FiltersDelegatingFilterProxyFilterChainProxySecurityFilterChainSecurity Filters 三…

Java学习指南:从基础到进阶,一篇文章带你全面了解!

一、Java语言概述 Java是一种面向对象的编程语言&#xff0c;它不仅吸收了C语言的各种优点&#xff0c;还摒弃了C里难以理解的多继承、指针等概念。Java语言具有功能强大和简单易用两个特征&#xff0c;即Java语言作为静态面向对象编程语言的代表&#xff0c;极好地实现了面向…

vue3-pinia使用(末尾有彩蛋)

什么是 pinia Pinia 是 Vue 的专属状态管理库&#xff0c;它允许你跨组件或页面共享状态。 之前用的是 vuex&#xff0c;后面 vue 官方团队不维护了&#xff0c;推荐使用 pinia 安装 yarn add pinia # 或者使用 npm npm install piniapnpm install piniaStore 是什么&#xf…

实验一 Python集成开发环境的搭建及可视化库的安装

一、安装集成开发环境 下载安装包 官方网址&#xff1a; Free Download | Anaconda 或者镜像网站下载&#xff08;较快&#xff09; https://repo.anaconda.com/archive/ 安装 配置环境变量 验证 输入&#xff1a; conda -V 二、下载pyecharts环境 点击 Anaconda Promp…

探索实战课程的魅力

在当今迅速变化的时代&#xff0c;学习不仅仅是获取知识&#xff0c;更是掌握实用技能的关键。随着在线学习的兴起&#xff0c;实战课程在培养学生实践能力方面发挥着越来越重要的作用。本文将探讨实战课程的魅力&#xff0c;以及它们对个人职业发展的巨大影响。 深度学习的机…

spring-boot之shiro安全框架配置使用

shiro架构&#xff08;外部&#xff09; shiro架构(内部) 具体API操作 获取当前的用户对象 Subject currentUser SecurityUtils.getSubject();通过当前用户拿到session Session session currentUser.getSession(); session.setAttribute("someKey", "aValu…