获取RGB值思路:截图,获取图片的(0,0)的RGB值
int x = QCursor::pos().x(); int y = QCursor::pos().y(); // QPixmap pixmap = QPixmap::grabWindow(QApplication::desktop()->winId(), x, y, 1, 1); QList<QScreen *> list_screen = QGuiApplication::screens(); //可能电脑接了多个屏幕 QPixmap pixmap = list_screen.at(0)->grabWindow(0,x,y,1,1); if (!pixmap.isNull()) //如果像素图不为NULL { QImage image = pixmap.toImage();//将像素图转换为QImage if (!image.isNull()) //如果image不为空 { if (image.valid(0, 0)) //坐标位置有效 { QColor color = image.pixel(0, 0); mousedPressed_R = color.red(); mousedPressed_G = color.green(); mousedPressed_B = color.blue(); QString text = QString("RGB: %1, %2, %3").arg(mousedPressed_R).arg(mousedPressed_G).arg(mousedPressed_B); qDebug() << text; } } }