通过qt实现一些截屏功能
qt截图API
qApp->primaryScreen()->grabWindow(0);
画框选API
void drawRect(int x1, int y1, int w, int h);
鼠标事件API
void mousePressEvent(QMouseEvent *event);
void mouseReleaseEvent(QMouseEvent *event);
void mouseDoubleClickEvent(QMouseEvent *event);
void mouseMoveEvent(QMouseEvent *event);
获取鼠标点
void CaptureWgt::mousePressEvent(QMouseEvent *event)
{
m_start = event->pos();
}
void CaptureWgt::mouseReleaseEvent(QMouseEvent *event)
{
m_end = event->pos();
}
绘制鼠标框选区域
painter.drawRect(m_start.x(),m_start.y(),m_end.x()-m_start.x(),m_end.y()-m_start.y());
保存到剪贴板
void CaptureWgt::capture()
{
int x = m_end.x() < m_start.x() ? m_end.x() : m_start.x();
int y = m_end.y() < m_start.y() ? m_end.y() : m_start.y();
m_pix = m_pix.copy(x,y,qAbs(m_end.x()-m_start.x()),qAbs(m_end.y()-m_start.y()));
}
头文件
#ifndef CAPTUREWGT_H
#define CAPTUREWGT_H
#include
#include
#include
class ToolWgt;
class CaptureWgt : public QWidget
{
Q_OBJECT
public:
CaptureWgt(QWidget *parent = nullptr);
~CaptureWgt();
protected:
void paintEvent(QPaintEvent*);
void mousePressEvent(QMouseEvent *event);
void mouseReleaseEvent(QMouseEvent *event);
void mouseDoubleClickEvent(QMouseEvent *event);
void mouseMoveEvent(QMouseEvent *event);
void keyPressEvent(QKeyEvent *event);
void updateToolWgt();
void capture();
private slots:
void slot_check();
void slot_close();
void slot_download();
private:
bool m_isGrap{false};
QPointF m_start;
QPointF m_end;
QPointF m_startTemp;
QPointF m_endTemp;
QPixmap m_pix;
ToolWgt* m_toolWgt;
};
#endif
源文件
#include "capturewgt.h"
#include "toolwgt.h"
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
CaptureWgt::CaptureWgt(QWidget *parent)
: QWidget(parent)
{
setWindowFlags(Qt::FramelessWindowHint |Qt::WindowStaysOnTopHint|Qt::Tool);
setAttribute(Qt::WA_TranslucentBackground);
m_toolWgt = new ToolWgt(this);
connect(m_toolWgt,&ToolWgt::sig_check,this,&CaptureWgt::slot_check);
connect(m_toolWgt,&ToolWgt::sig_close,this,&CaptureWgt::slot_close);
connect(m_toolWgt,&ToolWgt::sig_download,this,&CaptureWgt::slot_download);
m_pix = qApp->primaryScreen()->grabWindow(0);
this->grabKeyboard();
m_toolWgt->move((m_pix.width()-m_toolWgt->width()),0);
}
CaptureWgt::~CaptureWgt()
{
}
void CaptureWgt::paintEvent(QPaintEvent* event)
{
QPainter painter(this);
if(m_isGrap){
painter.drawRect(this->rect());
}else{
painter.drawPixmap(0,0,m_pix);
QPen pen = painter.pen();
pen.setColor(Qt::red);
painter.setRenderHint(QPainter::Antialiasing);
if(!m_start.isNull() && !m_end.isNull()){
pen.setWidth(1);
painter.setPen(pen);
painter.drawRect(m_start.x(),m_start.y(),m_end.x()-m_start.x(),m_end.y()-m_start.y());
}else{
pen.setWidth(pen.width()+3);
painter.setPen(pen);
painter.drawRect(0,0,width()-3,height()-5);
}
}
event->accept();
}
void CaptureWgt::mousePressEvent(QMouseEvent *event)
{
m_startTemp = m_start;
m_endTemp = m_end;
m_isGrap = true;
m_toolWgt->hide();
this->repaint();
m_pix = qApp->primaryScreen()->grabWindow(0);
m_isGrap = false;
m_start = event->pos();
m_end = QPointF();
updateToolWgt();
}
void CaptureWgt::mouseReleaseEvent(QMouseEvent *event)
{
m_end = event->pos();
if(qAbs( m_start.x() - m_end.x()) < 1 && qAbs( m_start.x() - m_end.x()) < 1){
m_end = m_endTemp;
m_start = m_startTemp;
}
update();
updateToolWgt();
}
void CaptureWgt::mouseDoubleClickEvent(QMouseEvent *event)
{
event->accept();
slot_check();
m_toolWgt->close();
}
void CaptureWgt::mouseMoveEvent(QMouseEvent *event)
{
m_end = event->pos();
update();
m_toolWgt->hide();
}
void CaptureWgt::keyPressEvent(QKeyEvent *event)
{
if(event->key() == Qt::Key_Space &&
event->modifiers() == (Qt::AltModifier|Qt::ControlModifier) ){
slot_download();
}
else if(event->key() == Qt::Key_Space &&
event->modifiers() == (Qt::ShiftModifier|Qt::ControlModifier) ){
slot_check();
}
event->accept();
}
void CaptureWgt::updateToolWgt()
{
if(m_start.isNull() || m_end.isNull()){
m_toolWgt->move((m_pix.width()-m_toolWgt->width()),0);
return;
}
if(m_start == m_end){
m_toolWgt->hide();
return;
}
int x = m_end.x() > m_start.x() ? m_end.x() : m_start.x();
int y = m_end.y() > m_start.y() ? m_end.y() : m_start.y();
if(y+m_toolWgt->height() > m_pix.height()){
y = 0;
}
m_toolWgt->setGeometry(x-m_toolWgt->width(),y,m_toolWgt->width(),m_toolWgt->height());
m_toolWgt->show();
}
void CaptureWgt::capture()
{
int x = m_end.x() < m_start.x() ? m_end.x() : m_start.x();
int y = m_end.y() < m_start.y() ? m_end.y() : m_start.y();
m_pix = m_pix.copy(x,y,qAbs(m_end.x()-m_start.x()),qAbs(m_end.y()-m_start.y()));
}
void CaptureWgt::slot_check()
{
this->capture();
if(false == m_pix.isNull()){
QClipboard* pClipboard = QApplication::clipboard();
pClipboard->setPixmap(m_pix);
}
this->close();
}
void CaptureWgt::slot_close()
{
this->close();
}
void CaptureWgt::slot_download()
{
do{
QString fileName = QStandardPaths::writableLocation(QStandardPaths::PicturesLocation)
+ QString("/IMG_")+QDateTime::currentDateTime().toString("yyyyMMddhhmmss")+QString(".jpg");
fileName = QFileDialog::getSaveFileName(this, tr("Save File"),
fileName,
tr("Images (*.png *.jpg)"));
if(fileName.isEmpty()){
break;
}
this->capture();
if(m_pix.isNull()){
break;
}
m_pix.save(fileName);
}while(0);
this->close();
}
按钮
#ifndef TOOLWGT_H
#define TOOLWGT_H
#include
class QToolButton;
class ToolWgt : public QWidget
{
Q_OBJECT
public:
explicit ToolWgt(QWidget *parent = nullptr);
QToolButton* create(QString res);
signals:
void sig_check();
void sig_close();
void sig_download();
};
#endif
#include "toolwgt.h"
#include
#include
#include
ToolWgt::ToolWgt(QWidget *parent)
: QWidget{parent}
{
QHBoxLayout* pLayout = new QHBoxLayout(this);
pLayout->setContentsMargins(5,5,0,5);
pLayout->addStretch();
QToolButton* pBtn = create(QStringLiteral(":/img/download.svg"));
pBtn->setToolTip(QStringLiteral("保存选中区域"));
connect(pBtn,&QToolButton::clicked,this,[&]{
emit sig_download();
this->close();
});
pLayout->addWidget(pBtn);
pBtn = create(QStringLiteral(":/img/close.svg"));
pBtn->setToolTip(QStringLiteral("取消本次截屏"));
connect(pBtn,&QToolButton::clicked,this,[&]{
emit sig_close();
this->close();
});
pLayout->addWidget(pBtn);
pBtn = create(QStringLiteral(":/img/check.svg"));
pBtn->setToolTip(QStringLiteral("完成"));
connect(pBtn,&QToolButton::clicked,this,[&]{
emit sig_check();
this->close();
});
pLayout->addWidget(pBtn);
adjustSize();
}
QToolButton *ToolWgt::create(QString res)
{
QToolButton* pBtn = new QToolButton;
pBtn->setIcon(QIcon(res));
pBtn->setIconSize(QSize(48,48));
pBtn->setFixedSize(48,48);
pBtn->setToolButtonStyle(Qt::ToolButtonIconOnly);
pBtn->setAutoRaise(true);
return pBtn;
}