Qt快捷屏幕截图

Qt4使用QPixmap::grabWindow接口获取指定屏幕;
Qt5使用QScreen 的grabWindow接口获取指定屏幕;
QApplication::desktop()获取根窗口;
QUuid::createUuid()产生唯一ID;
pixmap.save(name)保存截图到本地.

方法1

  • 接口:
  1. QPixmap QScreen::grabWindow(WId window,
  2. int x = 0,
  3. int y = 0,
  4. int width = -1,
  5. int height = -1)
QPixmap QScreen::grabWindow(WId window, 
                            int x = 0, 
                            int y = 0, 
                            int width = -1, 
                            int height = -1)
  • 示例:
  1. QScreen *screen = QGuiApplication::primaryScreen();
  2. screen->grabWindow(0).save("screen_shot.jpg", "jpg"); // 0值为整个电脑屏幕WId
QScreen *screen = QGuiApplication::primaryScreen();
screen->grabWindow(0).save("screen_shot.jpg", "jpg"); // 0值为整个电脑屏幕WId
  • 可以通过设置x,y坐标位置和width,height的大小来截图。

方法2

  • 接口:
  1. QPixmap QWidget::grab(const QRect &rectangle = QRect(QPoint(0, 0),
  2. QSize(-1, -1)))
QPixmap QWidget::grab(const QRect &rectangle = QRect(QPoint(0, 0), 
                      QSize(-1, -1)))
  • 示例:
  1. QWidget widget;
  2. widget.grab().save("screen_shot.jpg", "jpg");
QWidget widget;
widget.grab().save("screen_shot.jpg", "jpg");
  • 和QScreen的grabWindow一样可以通过设置坐标位置和窗口的大小来截图。

main.cpp

  1. #include <QApplication>
  2. #include <QDesktopWidget>
  3. #include <QScreen>
  4. #include <QPixmap>
  5. #include <QUuid>
  6. #include <QDateTime>
  7. int main(int argc, char *argv[])
  8. {
  9. QApplication a(argc, argv);
  10. #if (QT_VERSION < QT_VERSION_CHECK(5, 0, 0))
  11. QPixmap pixmap = QPixmap::grabWindow(QApplication::desktop()->winId());
  12. #else if (QT_VERSION >= QT_VERSION_CHECK(5, 0, 0))
  13. QScreen *screen = QGuiApplication::primaryScreen();
  14. QPixmap pixmap = screen->grabWindow(QApplication::desktop()->winId());
  15. #endif
  16. QString name = QString("%1-%2.png").
  17. arg(QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss")).
  18. arg(QUuid::createUuid().toString());
  19. pixmap.save(name);
  20. return 0;
  21. }
#include <QApplication>
#include <QDesktopWidget>
#include <QScreen>
#include <QPixmap>
#include <QUuid>
#include <QDateTime>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);

#if (QT_VERSION < QT_VERSION_CHECK(5, 0, 0))
    QPixmap pixmap = QPixmap::grabWindow(QApplication::desktop()->winId());
#else if (QT_VERSION >= QT_VERSION_CHECK(5, 0, 0))
    QScreen *screen = QGuiApplication::primaryScreen();
    QPixmap pixmap = screen->grabWindow(QApplication::desktop()->winId());
#endif

    QString name = QString("%1-%2.png").
            arg(QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss")).
            arg(QUuid::createUuid().toString());

    pixmap.save(name);

    return 0;
}

qmake文件

  1. QT += core
  2. equals(QT_MAJOR_VERSION, 5) : QT += widgets
  3. CONFIG -= app_bundle
  4. TARGET = Screenshot
  5. TEMPLATE = app
  6. SOURCES += main.cpp
QT += core

equals(QT_MAJOR_VERSION, 5) : QT += widgets

CONFIG -= app_bundle
TARGET = Screenshot

TEMPLATE = app

SOURCES += main.cpp

作者:

喜欢围棋和编程。

 
发布于 分类 编程标签

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注