qt截图代码怎么在linux里面使用方法,用QT实现类似QQ截图的工具

widget.cpp

#include "widget.h"

#include "ui_widget.h"

#include

Widget::Widget(QWidget *parent) :

QWidget(parent),

ui(new Ui::Widget)

{

ui->setupUi(this);

createWidgets();

createConnects();

createEventFilter();

}

Widget::~Widget()

{

delete ui;

delete quit;

delete mini;

delete restore;

delete menu;

delete trayIcon;

delete fullScreenLabel;

delete shotScreenLabel;

}

bool Widget::eventFilter(QObject *o, QEvent *e)

{

if (o != fullScreenLabel)

{

return Widget::eventFilter(o, e);

}

QMouseEvent *mouseEvent = static_cast (e);

//true 鼠标左键按下且按键还未弹起

if ((mouseEvent->button() == Qt::LeftButton)

&& (mouseEvent->type() == QEvent::MouseButtonPress))

{

//鼠标左键标志位按下

leftMousePress = true;

//获取鼠标点

origin = mouseEvent->pos();

if (!rubberBand)

{

rubberBand = new QRubberBand(QRubberBand::Rectangle, fullScreenLabel);

}

rubberBand->setGeometry(QRect(origin,QSize()));

rubberBand->show();

return true;

}

//true 鼠标左键按下并拖动

if ((mouseEvent->type() == QEvent::MouseMove)

&& (leftMousePress))

{

if (rubberBand)

{

rubberBand->setGeometry(QRect(origin, mouseEvent->pos()).normalized());

}

return true;

}

//鼠标左键松开

if ((mouseEvent->button() == Qt::LeftButton)

&& (mouseEvent->type() == QEvent::MouseButtonRelease))

{

//鼠标标志位弹起

leftMousePress = false;

if (rubberBand)

{

//获取橡皮筋框的终止坐标

termination = mouseEvent->pos();

QRect rect = QRect(origin, termination);

//根据橡皮筋框截取全屏上的信息,并将其放入shotScreenLabel

shotScreenLabel->setPixmap(fullScreenPixmap.grabWidget(fullScreenLabel,

rect.x(),

rect.y(),

rect.width(),

rect.height()));

//将shotScreenLabel的用户区大小固定为所截图片大小

shotScreenLabel->setFixedSize(rect.width(), rect.height());

shotScreenLabel->show();

rubberBand->hide();

fullScreenLabel->hide();

}

return true;

}

return false;

}

/**

descr:实例化控件

*/

void Widget::createWidgets()

{

//两个QLabel的父控件不能为this,否则截图信息会现在是主窗口中,无法正确显示

fullScreenLabel = new QLabel();

shotScreenLabel = new QLabel();

rubberBand = new QRubberBand(QRubberBand::Rectangle, fullScreenLabel);

leftMousePress = false;

//初始化托盘控件并组装**************************************************************

trayIcon = new QSystemTrayIcon(QIcon(tr(":/images/heart.svg")), this);

menu = new QMenu(this);

restore = new QAction(tr("Restore"), this);

mini = new QAction(tr("Mini"), this);

quit = new QAction(tr("Quit"), this);

menu->addAction(restore);

menu->addAction(mini);

menu->addAction(quit);

trayIcon->setContextMenu(menu);

//将托盘显示

trayIcon->show();

//初始化托盘控件并组装**************************************************************

savePixmap = new QAction(tr("save"), shotScreenLabel);

shotScreenLabel->addAction(savePixmap);

shotScreenLabel->setContextMenuPolicy(Qt::ActionsContextMenu);

}

void Widget::createConnects()

{

//主窗口信号槽*****************************************************************

connect(ui->pbtnShot, SIGNAL(clicked()), this, SLOT(grapWindowScreen()));

connect(ui->pbtnShotAndMin, SIGNAL(clicked()), this, SLOT(miniWindows()));

connect(ui->pbtnMin, SIGNAL(clicked()), this, SLOT(miniWindows()));

connect(savePixmap, SIGNAL(triggered()), this, SLOT(saveShotPixmap()));

//主窗口信号槽*****************************************************************

//托盘信号槽*******************************************************************

connect(restore, SIGNAL(triggered()), this, SLOT(restoreWindows()));

connect(mini, SIGNAL(triggered()), this, SLOT(miniWindows()));

connect(quit, SIGNAL(triggered()), this, SLOT(quitApplication()));

//托盘信号槽*******************************************************************

}

void Widget::createEventFilter()

{

fullScreenLabel->installEventFilter(this);

}

QString Widget::getSaveShotPixmap()

{

return QFileDialog::getSaveFileName(shotScreenLabel,

tr("Open Image"),

".",

tr("Image Files(*.JPG *.PNG)"));

}

void Widget::grapWindowScreen()

{

if (!fullScreenLabel)

{

fullScreenLabel = new QLabel();

}

//获取全屏截图fullScreenPixmap,并将其放入fullScreenLabel

fullScreenPixmap = QPixmap::grabWindow(QApplication::desktop()->winId());

fullScreenLabel->setPixmap(fullScreenPixmap);

//label全屏显示

fullScreenLabel->showFullScreen();

}

void Widget::miniWindows()

{

showMinimized();

grapWindowScreen();

}

void Widget::restoreWindows()

{

showNormal();

}

void Widget::quitApplication()

{

qApp->quit();

}

void Widget::saveShotPixmap()

{

QString fileName = getSaveShotPixmap();

if (!fileName.isNull())

{

fullScreenPixmap.save(fileName);

}

}

0b1331709591d260c1c78e86d0c51c18.png

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值