Qt用截图方式做一个颜色拾取器

一、首先画一个鼠标矩形框,由此新建一个MouseRect 类**
//
MouseRect::MouseRect(std::shared_ptr screenshort,
QWidget *parent) : QWidget(parent),
m_screenshort(screenshort)
{
setMouseTracking(true);
setFixedSize(imageW,imageH);
hide();
}
void MouseRect::onRectPostionChange(int x, int y) {
m_cursorPoint = QPoint(x, y);
raise();
int dest_x = x + 4;
int dest_y = y + 26;

//Exceeding screen detection
const QSize& parent_size = parentWidget()->size();
if (dest_y + height() > parent_size.height()) {
    dest_y = y - 26 - height();
}
if (dest_x + width() > parent_size.width()) {
    dest_x = x - 4 - width();
}

move(dest_x, dest_y);

}
void MouseRect::paintEvent(QPaintEvent *) {
QPainter painter(this);
painter.fillRect(rect(), QColor(0, 0, 0, 160));
QImage image = m_screenshort->toImage();
cursor_pixel = image.pixel(m_cursorPoint);
QString select_pt_rgb = QString(“RGB:(%1,%2,%3)”)
.arg(cursor_pixel.red())
.arg(cursor_pixel.green())
.arg(cursor_pixel.blue());
QString select_pt_cmy = QString(“CMY:(%1,%2,%3)”)
.arg(255-(cursor_pixel.red()))
.arg(255-(cursor_pixel.green()))
.arg(255-(cursor_pixel.blue()));

painter.fillRect(0,0,width(), height()-35, cursor_pixel);
painter.setPen(QPen(Qt::darkGreen, 1));
painter.drawRect(0,0,width()-1,height()-1);
painter.setPen(Qt::white);
painter.drawText(QPoint(6, height()-24),select_pt_rgb);
painter.drawText(QPoint(6, height()-7),select_pt_cmy);

}

二、处理截图类Screenshot 并获取图像传到MouseRect 类
//
Screenshot::Screenshot(QWidget *parent) : QWidget(parent),
m_backgroundScreen(NULL),m_screenShort(NULL)
{
initCursor();
initGlobalScreen();
initColorRect();
showFullScreen();
setMouseTracking(true);
emit cursorPosChange(cursor().pos().x(), cursor().pos().y());
show();
}
Screenshot::~Screenshot(void)
{

}
void Screenshot::initCursor()
{
setCursor(Qt::ArrowCursor);
}
std::shared_ptr Screenshot::initGlobalScreen(void) {
if (m_backgroundScreen.get() != NULL) {
return m_backgroundScreen;
}

std::shared_ptr<QPixmap> temp_screen = getGlobalScreen();
m_backgroundScreen.reset(new QPixmap(*temp_screen));
return m_backgroundScreen;

}
std::shared_ptr Screenshot::getGlobalScreen(void) {
if (m_screenShort.get() == NULL) {
QScreen *screen = QGuiApplication::primaryScreen();
const QRect& temp_rect = getScreenRect();
m_screenShort.reset(new QPixmap(screen->grabWindow(0, temp_rect.x(),
temp_rect.y(), temp_rect.width(),
temp_rect.height())));
}
return m_screenShort;
}
void Screenshot::initColorRect(std::shared_ptr originPainting)
{
std::shared_ptr temp_pm = originPainting;
if (temp_pm == NULL) {
temp_pm = m_screenShort;
}
m_colorRect.reset(new MouseRect(temp_pm, this));
connect(this,SIGNAL(cursorPosChange(int,int)),
m_colorRect.get(), SLOT(onRectPostionChange(int,int)));
m_colorRect->show();
m_colorRect->raise();
}

void Screenshot::paintEvent(QPaintEvent *)
{
QPainter painter(this);
painter.drawPixmap(0,0,desktopRect_.width(),
desktopRect_.height(), *m_backgroundScreen);

}
const QRect &Screenshot::getScreenRect(void)
{
if (!desktopRect_.isEmpty()) {
return desktopRect_;
}
desktopRect_ = QRect(QApplication::desktop()->pos(),
QApplication::desktop()->size());
return desktopRect_;
}
void Screenshot::mouseMoveEvent(QMouseEvent *e)
{
emit cursorPosChange(e->x(), e->y());
QWidget::mouseMoveEvent(e);
}

void Screenshot::mousePressEvent(QMouseEvent *e)
{
if (e->button() == Qt::LeftButton) {
emit screenColor(m_colorRect->cursor_pixel);
m_colorRect->setMouseTracking(false);
close();
return ;
}
}
void Screenshot::keyPressEvent(QKeyEvent *e)
{
if (e->key() == Qt::Key_Escape) {
m_colorRect->setMouseTracking(false);
close();
}
else if(e->key() == Qt::Key_Alt){
emit screenColor(m_colorRect->cursor_pixel);
close();
}
else{
e->ignore();
}
}
三、调用PickColor
//
void PickColor::onClickPickColor()
{
m_pickupColor = new Screenshot;
connect(m_pickupColor,SIGNAL(screenColor(QColor)),
this,SLOT(onPickColorChanged(QColor)));
}
void PickColor::onPickColorChanged( QColor pickupColr)
{
qDebug()<< “pickupColr:” << pickupColr;
}
四、效果图
在这里插入图片描述

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

crushFlower

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值