Qt浅谈之抓图截屏
来源:互联网 发布:易语言端口转发源码 编辑:程序博客网 时间:2024/05/26 09:55
一、简介
因Windows下无法登录QQ时就不能抓图了,就很纠结了,而在Linux下,有时也无法抓图。所以专门使用Qt仿照Centos的抓图界面写了一个抓图软件,能实现自己的大部分需求。感兴趣的可以扩展到图片的分割组合及录屏等功能。
二、运行图
(1)本程序运行如下图1所示。
(2)其他相关截图程序的界面,如下图2所示。
三、详解
1、中文问题
(1)不同的操作系统采用编码方式不同,因此必须使用国际化翻译才能减少乱码的实现。其过程如下:
1.pro工程文件里面添加 TRANSLATIONS+=mypro.ts
2.选择Qt Creator环境的菜单栏 工具->外部->Qt语言家->更新翻译
3.桌面开始菜单里面Qt目录打开 Linguist工具
4.Linguist工具加载生成好的mypro.ts文件
5.填好翻译, 保存, Release, 就生成好编译后的qm文件
6.在工程的源文件中, 这样加载qm文件:
QTranslator translator;
translator.load("grabwindows.qm",":/");
a.installTranslator(&translator);
2、系统托盘
- void GrabWindows::initTrayiconMenu()
- {
- trayIcon = new QSystemTrayIcon(QIcon(tr(":/applets-screenshooter.ico")), this);
- trayiconMenu = new QMenu;
- restoreAction = new QAction(tr("Restore"), this);
- quitAction = new QAction(tr("Quit"), this);
- trayiconMenu->addAction(restoreAction);
- trayiconMenu->addAction(quitAction);
- trayIcon->setContextMenu(trayiconMenu);
- trayIcon->show();
- connect(trayIcon,SIGNAL(activated(QSystemTrayIcon::ActivationReason)),
- this,SLOT(slotIconActivated(QSystemTrayIcon::ActivationReason)));
- connect(restoreAction,SIGNAL(triggered()),this,SLOT(showNormal()));
- connect(quitAction,SIGNAL(triggered()),this,SLOT(close()));
- }
分析:QSystemTrayIcon的使用,并结合槽来捕获鼠标左键的信号,鼠标右键信号会自动弹出定义的右键菜单。
3、抓取全屏
- fullScreenPixmap = QPixmap::grabWindow(QApplication::desktop()->winId());
- fullScreenPixmap->save("fullScreen.jpg","JPG");
- 或
- fullScreenPixmap->save("Screenshot.png","png");
只要完成了布局,剩下的截图代码非常简单,Qt已经做了很好的封装。
4、抓取当前窗口
- if (QApplication::activeWindow()) {
- currentScreenPixmap = QPixmap::grabWindow( QApplication::activeWindow()->winId(), -2, -26, QApplication::activeWindow()->width() + 4, QApplication::activeWindow()->height() + 30);
- //currentScreenPixmap = QPixmap::grabWindow( QApplication::activeWindow()->winId());
- }
5、抓取任意部分
- ScreenShotPart::ScreenShotPart(QWidget *parent) :
- QDialog(parent)
- {
- //setMouseTracking(true);
- isDrawing = false;
- fullScreenPix = QPixmap::grabWindow(QApplication::desktop()->winId());
- fullTempPix = fullScreenPix;
- }
- void ScreenShotPart::showEvent(QShowEvent *event)
- {
- showFullScreen();
- setCursor(Qt::CrossCursor);
- }
- void ScreenShotPart::paintEvent(QPaintEvent *event)
- {
- QPainter painter(this);
- painter.drawPixmap(0, 0, fullTempPix);
- QDialog::paintEvent(event);
- }
- void ScreenShotPart::mousePressEvent(QMouseEvent *event)
- {
- if (event->button() == Qt::LeftButton) {
- isDrawing = true;
- startPoint = event->pos();
- }
- }
- void ScreenShotPart::mouseMoveEvent(QMouseEvent *event)
- {
- QPoint pt = event->pos();
- if (isDrawing) {
- fullTempPix = fullScreenPix;
- endPonit = event->pos();
- paint(fullTempPix);
- }
- }
- void ScreenShotPart::mouseReleaseEvent(QMouseEvent *event)
- {
- if (event->button() == Qt::LeftButton) {
- isDrawing = false;
- endPonit = event->pos();
- shotRect = QRect(startPoint, endPonit);
- paint(fullTempPix);
- savePixMap();
- }
- }
6、定时器
- timer = new QTimer(this);
- timer->start(200 + timeComBox->value() * 1000);
四、不足之处
(1)无法很好的抓取当前窗口,正在考虑如何截取本窗口的下一层窗体。在Windows下截图的当前窗口为空白。
(2)ComboBox下的文件系统路径无法使用,加载了文件系统也无法一次进行选择,效果图也不是很美观,这个问题还待深究。截图图片暂时只能保存到当前程序的运行目录下。
- Qt浅谈之抓图截屏
- Qt浅谈之十三:抓图截屏
- Qt浅谈之十三:抓图截屏
- Qt浅谈之总结(整理)
- Qt浅谈之日历(QCalendarWidget)
- Qt浅谈之电子时钟
- Qt浅谈之五十一QT_OpenGL
- Qt浅谈之总结(整理)
- Qt浅谈之九:Qt鼠标
- Qt浅谈之Qt各类位置信息
- Qt浅谈之九:Qt鼠标
- Qt浅谈之二十九Qt多线程
- Qt浅谈之二:钟表(时分秒针)
- Qt浅谈之三:内置标准对话框
- Qt浅谈之四:电子时钟
- Qt浅谈之六:日历(QCalendarWidget)
- Qt浅谈之十四:文件操作
- Qt浅谈之十五:键盘控制移动
- 我的助理辞职了—刘苏
- Qt浅谈之十五:键盘控制移动
- Spring整合Hibernate
- MySql与Java的时间类型
- 算法剖析之选择排序与插入排序
- Qt浅谈之抓图截屏
- Java自学视频整理(持续更新中...)
- 域名naichabiao.com和京东是怎么联系上的
- CentOS下Qt窗口透明效果失效,成黑色背景的问题
- 巧用HTML标签优化XML中的布局
- android linux 对应笔记
- 把给定的本地时间批量转成UTC时间
- Qt浅谈之拖拽文字图片
- STL 之deque (江南烟雨)