一、效果展示

二、具体代码
#ifndef
#define
#include <QMainWindow>
#include <QLabel>
#include <QScreen>
#include <QDesktopWidget>
#include <QPixmap>
#include <QCoreApplication>
#include <QApplication>
#include <QDebug>
#include <QPropertyAnimation>
#include <QParallelAnimationGroup>
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
void msecSleep(int msec);
private slots:
void on_pushButton_clicked();
void animation_finished();
private:
Ui::MainWindow *ui;
QLabel* m_shot_label;
QPixmap m_shot_picture;
};
#endif// MAINWINDOW_H
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_pushButton_clicked()
{
m_shot_label = new QLabel(this);
m_shot_label->resize(this->width(),this->height());
m_shot_label->setStyleSheet("border: 2px dashed black;");
//m_shot_picture = QPixmap::grabWidget(this);
m_shot_picture = this->grab();
m_shot_label->setPixmap(m_shot_picture);
m_shot_label->show();
QPropertyAnimation* animation1 = new QPropertyAnimation(m_shot_label,"geometry");
animation1->setDuration(1000);
animation1->setEasingCurve(QEasingCurve::InOutExpo);
animation1->setStartValue(QRect(0,0,m_shot_label->width(),m_shot_label->height()));
animation1->setEndValue(QRect(3*m_shot_label->width()/4,0,m_shot_label->width()/4,m_shot_label->height()/4));
animation1->start();
connect(animation1,SIGNAL(finished()),this,SLOT(animation_finished()));
}
void MainWindow::animation_finished()
{
QDateTime current_date_time =QDateTime::currentDateTime();
QString current_date =current_date_time.toString("yyyy_MM_dd_hh_mm_ss");
m_shot_picture.save(QCoreApplication::applicationDirPath()+"/ShotCut"+current_date+".jpg");
msecSleep(1000);
delete m_shot_label;
m_shot_label = nullptr;
}
void MainWindow::msecSleep(int msec)
{
QTime dieTime = QTime::currentTime().addMSecs(msec);
while( QTime::currentTime() <dieTime )
QCoreApplication::processEvents(QEventLoop::AllEvents, 100);
}
                
 
 
                     
            
        













 
                    

 
                 
                    