延时抓图保存
Posted kuikuitage
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了延时抓图保存相关的知识,希望对你有一定的参考价值。
snap.pro
#-------------------------------------------------
#
# Project created by QtCreator 2020-05-02T15:51:11
#
#-------------------------------------------------
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = snap
TEMPLATE = app
# The following define makes your compiler emit warnings if you use
# any feature of Qt which has been marked as deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS
# You can also make your code fail to compile if you use deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
CONFIG += c++11
SOURCES += main.cpp snap.cpp
HEADERS += snap.h
FORMS += snap.ui
# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target
snap.h
#ifndef SNAP_H
#define SNAP_H
#include <QMainWindow>
#include <QTimer>
#include <QPixmap>
namespace Ui {
class snap;
}
class snap : public QMainWindow
{
Q_OBJECT
public:
explicit snap(QWidget *parent = nullptr);
~snap();
public slots:
void onClickNewSnapSlot();
void onTimerSnapSlot();
void onClickSaveSnapSlot();
private:
Ui::snap *ui;
QTimer* m_timer;
QPixmap m_pixMap;
};
#endif // SNAP_H
snap.cpp
#include "snap.h"
#include "ui_snap.h"
#include <QPixmap>
#include <QApplication>
#include <QDesktopWidget>
#include <QFileDialog>
snap::snap(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::snap)
{
ui->setupUi(this);
connect(ui->newBtn, SIGNAL(clicked()), this, SLOT(onClickNewSnapSlot()));
connect(ui->saveBtn, SIGNAL(clicked()), this, SLOT(onClickSaveSnapSlot()));
connect(ui->exitBtn, SIGNAL(clicked()), this, SLOT(close()));
}
snap::~snap()
{
delete ui;
}
void snap::onClickNewSnapSlot()
{
if(ui->isDelayCheckBox->isChecked())
{
this->hide();
m_timer = new QTimer();
connect(this->m_timer, SIGNAL(timeout()), this, SLOT(onTimerSnapSlot()));
m_timer->start(ui->delayTimeSpinBox->value()*1000);
}
else
{
qApp->beep();
}
}
void snap::onClickSaveSnapSlot()
{
QString fileName = QFileDialog::getSaveFileName(this, "save file", QDir::currentPath(), tr("Images (*.png *.xpm *.jpg)"));
m_pixMap.save(fileName);
}
void snap::onTimerSnapSlot()
{
this->m_pixMap = QPixmap::grabWindow(QApplication::desktop()->winId(), 0, 0, -1, -1);
ui->showLabel->setPixmap(this->m_pixMap.scaled(ui->showLabel->size()));
this->show();
m_timer->stop();
}
main.cpp
#include "snap.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
snap w;
w.show();
return a.exec();
}
以上是关于延时抓图保存的主要内容,如果未能解决你的问题,请参考以下文章