生活随笔
收集整理的這篇文章主要介紹了
Qt实现截图的源码
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
Qt實現截圖的源碼
本人最近業務 的需求,需要寫一個截圖軟件,使用C++ Qt實現,經過查詢和知識的積累,才做出來
最終實現效果如圖
源代碼如下
#include "widget.h"
#include "ui_widget.h"#include <QDebug>
#include <QScreen>
#include <QMessageBox>
#include <QDateTime>
#include <QDir>
#include <QDesktopWidget>
#include <QBuffer>
#include <QFile>
#include <QCoreApplication>
#include <QString>
#include <QTime>Widget
::Widget(QWidget
*parent
) :QWidget(parent
),ui(new Ui
::Widget
)
{ui
->setupUi(this);this->setWindowIcon(QPixmap(":/images/internet-web-browser.png"));this->setAutoFillBackground(true);this->setWindowTitle("Photo Station 20200807");ui
->tabWidget
->setCurrentIndex(2);ui
->tabWidget
->setFixedHeight(300);this->setWindowFlags(this->windowFlags() | Qt
::WindowStaysOnTopHint
);connect(ui
->lineEditP12
,SIGNAL(returnPressed()),this,SLOT(on_pushButtonP12_clicked()));connect(ui
->lineEditQ12
,SIGNAL(returnPressed()),this,SLOT(on_pushButtonQ12_clicked()));connect(ui
->lineEditR12
,SIGNAL(returnPressed()),this,SLOT(on_pushButtonR12_clicked()));connect(ui
->lineEditH12
,SIGNAL(returnPressed()),this,SLOT(on_pushButtonH12_clicked()));connect(ui
->lineEditI12
,SIGNAL(returnPressed()),this,SLOT(on_pushButtonI12_clicked()));connect(ui
->lineEditI22
,SIGNAL(returnPressed()),this,SLOT(on_pushButtonI22_clicked()));connect(ui
->lineEditK12
,SIGNAL(returnPressed()),this,SLOT(on_pushButtonK12_clicked()));connect(ui
->lineEditJ12
,SIGNAL(returnPressed()),this,SLOT(on_pushButtonJ12_clicked()));connect(ui
->lineEditL12
,SIGNAL(returnPressed()),this,SLOT(on_pushButtonL12_clicked()));connect(ui
->lineEditL22
,SIGNAL(returnPressed()),this,SLOT(on_pushButtonL22_clicked()));connect(ui
->lineEditK22
,SIGNAL(returnPressed()),this,SLOT(on_pushButtonK22_clicked()));}Widget
::~Widget()
{delete ui
;
}
void Widget
::on_tabWidget_tabBarClicked(int index
)
{if(index
==0){ui
->tabWidget
->setFixedHeight(140);this->resize(230,160);}if(index
==1){ui
->tabWidget
->setFixedHeight(280);this->resize(230,300);}if(index
==2){ui
->tabWidget
->setFixedHeight(280);this->resize(230,300);}
}void Widget
::on_pushButtonP12_clicked()
{QString strPone
= ui
->lineEditP12
->text().toUtf8();if(strPone
.isEmpty()){QMessageBox
::warning(this,"error","錯誤,必須輸入SN");return;}QString pathTime
= QDateTime
::currentDateTime().toString("yyyy-MM-dd HH:mm:ss");QString pathTime2
= QDateTime
::currentDateTime().toString("yyyy-MM-dd");QString paths
= QString("%1 20:00:00").arg(pathTime2
);QDateTime nowTime
= QDateTime
::fromString(pathTime
,"yyyy-MM-dd HH:mm:ss");QDateTime fixedTime
= QDateTime
::fromString(paths
,"yyyy-MM-dd HH:mm:ss");uint nowTime_s
= nowTime
.toTime_t();uint fixedTime_s
= fixedTime
.toTime_t();int c
= fixedTime_s
-nowTime_s
;QScreen
*screen
= QGuiApplication
::primaryScreen();if(c
>0){QString datePath
= QDateTime
::currentDateTime().toString("MMdd");QString path
= QString("C:\\P12\\%1").arg(datePath
);QString childDir
= ui
->lineEditPChilder12
->text().toUtf8();if(!childDir
.isEmpty()){path
= QString("C:\\P12\\%1\\%2").arg(datePath
).arg(childDir
);}QDir
dir(path
);if(!dir
.exists()){dir
.mkdir(path
);}QString filePathName
=NULL;if(!childDir
.isEmpty()){filePathName
= QString("C:\\P12\\%1\\%2\\%3___").arg(datePath
).arg(childDir
).arg(strPone
);filePathName
+=QDateTime
::currentDateTime().toString("zzzz");filePathName
+=".jpg";screen
->grabWindow(0).save(filePathName
,"jpg");ui
->lineEditP12
->setText("");}else{filePathName
= QString("C:\\P12\\%1\\%2___").arg(datePath
).arg(strPone
);filePathName
+=QDateTime
::currentDateTime().toString("zzzz");filePathName
+=".jpg";screen
->grabWindow(0).save(filePathName
,"jpg");ui
->lineEditP12
->setText("");}}if(c
<0){QString datePath
= QDateTime
::currentDateTime().addDays(+1).toString("MMdd");QString path
= QString("C:\\P12\\%1").arg(datePath
);QString childDir
= ui
->lineEditPChilder12
->text().toUtf8();if(!childDir
.isEmpty()){path
= QString("C:\\P12\\%1\\%2").arg(datePath
).arg(childDir
);}QDir
dir(path
);if(!dir
.exists()){dir
.mkdir(path
);}QString filePathName
=NULL;if(!childDir
.isEmpty()){filePathName
= QString("C:\\P12\\%1\\%2\\%3___").arg(datePath
).arg(childDir
).arg(strPone
);filePathName
+=QDateTime
::currentDateTime().toString("zzzz");filePathName
+=".jpg";screen
->grabWindow(0).save(filePathName
,"jpg");ui
->lineEditP12
->setText("");}else{filePathName
= QString("C:\\P12\\%1\\%2___").arg(datePath
).arg(strPone
);filePathName
+=QDateTime
::currentDateTime().toString("zzzz");filePathName
+=".jpg";screen
->grabWindow(0).save(filePathName
,"jpg");ui
->lineEditP12
->setText("");}}
}void Widget
::on_pushButtonQ12_clicked()
{QString strPone
= ui
->lineEditQ12
->text().toUtf8();if(strPone
.isEmpty()){QMessageBox
::warning(this,"error","錯誤,必須輸入SN");return;}QString pathTime
= QDateTime
::currentDateTime().toString("yyyy-MM-dd HH:mm:ss");QString pathTime2
= QDateTime
::currentDateTime().toString("yyyy-MM-dd");QString paths
= QString("%1 20:00:00").arg(pathTime2
);QDateTime nowTime
= QDateTime
::fromString(pathTime
,"yyyy-MM-dd HH:mm:ss");QDateTime fixedTime
= QDateTime
::fromString(paths
,"yyyy-MM-dd HH:mm:ss");uint nowTime_s
= nowTime
.toTime_t();uint fixedTime_s
= fixedTime
.toTime_t();int c
= fixedTime_s
-nowTime_s
;QScreen
*screen
= QGuiApplication
::primaryScreen();if(c
>0){QString datePath
= QDateTime
::currentDateTime().toString("MMdd");QString path
= QString("C:\\Q12\\%1").arg(datePath
);QString childDir
= ui
->lineEditQChilder12
->text().toUtf8();if(!childDir
.isEmpty()){path
= QString("C:\\Q12\\%1\\%2").arg(datePath
).arg(childDir
);}QDir
dir(path
);if(!dir
.exists()){dir
.mkdir(path
);}QString filePathName
=NULL;if(!childDir
.isEmpty()){filePathName
= QString("C:\\Q12\\%1\\%2\\%3___").arg(datePath
).arg(childDir
).arg(strPone
);filePathName
+=QDateTime
::currentDateTime().toString("zzzz");filePathName
+=".jpg";screen
->grabWindow(0).save(filePathName
,"jpg");ui
->lineEditQ12
->setText("");}else{filePathName
= QString("C:\\Q12\\%1\\%2___").arg(datePath
).arg(strPone
);filePathName
+=QDateTime
::currentDateTime().toString("zzzz");filePathName
+=".jpg";screen
->grabWindow(0).save(filePathName
,"jpg");ui
->lineEditQ12
->setText("");}}if(c
<0){QString datePath
= QDateTime
::currentDateTime().addDays(+1).toString("MMdd");QString path
= QString("C:\\Q12\\%1").arg(datePath
);QString childDir
= ui
->lineEditQChilder12
->text().toUtf8();if(!childDir
.isEmpty()){path
= QString("C:\\Q12\\%1\\%2").arg(datePath
).arg(childDir
);}QDir
dir(path
);if(!dir
.exists()){dir
.mkdir(path
);}QString filePathName
=NULL;if(!childDir
.isEmpty()){filePathName
= QString("C:\\Q12\\%1\\%2\\%3___").arg(datePath
).arg(childDir
).arg(strPone
);filePathName
+=QDateTime
::currentDateTime().toString("zzzz");filePathName
+=".jpg";screen
->grabWindow(0).save(filePathName
,"jpg");ui
->lineEditQ12
->setText("");}else{filePathName
= QString("C:\\Q12\\%1\\%2___").arg(datePath
).arg(strPone
);filePathName
+=QDateTime
::currentDateTime().toString("zzzz");filePathName
+=".jpg";screen
->grabWindow(0).save(filePathName
,"jpg");ui
->lineEditQ12
->setText("");}}}void Widget
::on_pushButtonR12_clicked()
{QString strPone
= ui
->lineEditR12
->text().toUtf8();if(strPone
.isEmpty()){QMessageBox
::warning(this,"error","錯誤,必須輸入SN");return;}QString pathTime
= QDateTime
::currentDateTime().toString("yyyy-MM-dd HH:mm:ss");QString pathTime2
= QDateTime
::currentDateTime().toString("yyyy-MM-dd");QString paths
= QString("%1 20:00:00").arg(pathTime2
);QDateTime nowTime
= QDateTime
::fromString(pathTime
,"yyyy-MM-dd HH:mm:ss");QDateTime fixedTime
= QDateTime
::fromString(paths
,"yyyy-MM-dd HH:mm:ss");uint nowTime_s
= nowTime
.toTime_t();uint fixedTime_s
= fixedTime
.toTime_t();int c
= fixedTime_s
-nowTime_s
;QScreen
*screen
= QGuiApplication
::primaryScreen();if(c
>0){QString datePath
= QDateTime
::currentDateTime().toString("MMdd");QString path
= QString("C:\\R12\\%1").arg(datePath
);QString childDir
= ui
->lineEditRChilder12
->text().toUtf8();if(!childDir
.isEmpty()){path
= QString("C:\\R12\\%1\\%2").arg(datePath
).arg(childDir
);}QDir
dir(path
);if(!dir
.exists()){dir
.mkdir(path
);}QString filePathName
=NULL;if(!childDir
.isEmpty()){filePathName
= QString("C:\\R12\\%1\\%2\\%3___").arg(datePath
).arg(childDir
).arg(strPone
);filePathName
+=QDateTime
::currentDateTime().toString("zzzz");filePathName
+=".jpg";screen
->grabWindow(0).save(filePathName
,"jpg");ui
->lineEditR12
->setText("");}else{filePathName
= QString("C:\\R12\\%1\\%2___").arg(datePath
).arg(strPone
);filePathName
+=QDateTime
::currentDateTime().toString("zzzz");filePathName
+=".jpg";screen
->grabWindow(0).save(filePathName
,"jpg");ui
->lineEditR12
->setText("");}}if(c
<0){QString datePath
= QDateTime
::currentDateTime().addDays(+1).toString("MMdd");QString path
= QString("C:\\R12\\%1").arg(datePath
);QString childDir
= ui
->lineEditRChilder12
->text().toUtf8();if(!childDir
.isEmpty()){path
= QString("C:\\R12\\%1\\%2").arg(datePath
).arg(childDir
);}QDir
dir(path
);if(!dir
.exists()){dir
.mkdir(path
);}QString filePathName
=NULL;if(!childDir
.isEmpty()){filePathName
= QString("C:\\R12\\%1\\%2\\%3___").arg(datePath
).arg(childDir
).arg(strPone
);filePathName
+=QDateTime
::currentDateTime().toString("zzzz");filePathName
+=".jpg";screen
->grabWindow(0).save(filePathName
,"jpg");ui
->lineEditR12
->setText("");}else{filePathName
= QString("C:\\R12\\%1\\%2___").arg(datePath
).arg(strPone
);filePathName
+=QDateTime
::currentDateTime().toString("zzzz");filePathName
+=".jpg";screen
->grabWindow(0).save(filePathName
,"jpg");ui
->lineEditR12
->setText("");}}}void Widget
::on_pushButtonH12_clicked()
{QString strPone
= ui
->lineEditH12
->text().toUtf8();if(strPone
.isEmpty()){QMessageBox
::warning(this,"error","錯誤,必須輸入SN");return;}QString pathTime
= QDateTime
::currentDateTime().toString("yyyy-MM-dd HH:mm:ss");QString pathTime2
= QDateTime
::currentDateTime().toString("yyyy-MM-dd");QString paths
= QString("%1 20:00:00").arg(pathTime2
);QDateTime nowTime
= QDateTime
::fromString(pathTime
,"yyyy-MM-dd HH:mm:ss");QDateTime fixedTime
= QDateTime
::fromString(paths
,"yyyy-MM-dd HH:mm:ss");uint nowTime_s
= nowTime
.toTime_t();uint fixedTime_s
= fixedTime
.toTime_t();int c
= fixedTime_s
-nowTime_s
;QScreen
*screen
= QGuiApplication
::primaryScreen();if(c
>0){QString datePath
= QDateTime
::currentDateTime().toString("MMdd");QString path
= QString("C:\\H12\\%1").arg(datePath
);QString childDir
= ui
->lineEditHChilder12
->text().toUtf8();if(!childDir
.isEmpty()){path
= QString("C:\\H12\\%1\\%2").arg(datePath
).arg(childDir
);}QDir
dir(path
);if(!dir
.exists()){dir
.mkdir(path
);}QString filePathName
=NULL;if(!childDir
.isEmpty()){filePathName
= QString("C:\\H12\\%1\\%2\\%3___").arg(datePath
).arg(childDir
).arg(strPone
);filePathName
+=QDateTime
::currentDateTime().toString("zzzz");filePathName
+=".jpg";screen
->grabWindow(0).save(filePathName
,"jpg");ui
->lineEditH12
->setText("");}else{filePathName
= QString("C:\\H12\\%1\\%2___").arg(datePath
).arg(strPone
);filePathName
+=QDateTime
::currentDateTime().toString("zzzz");filePathName
+=".jpg";screen
->grabWindow(0).save(filePathName
,"jpg");ui
->lineEditH12
->setText("");}}if(c
<0){QString datePath
= QDateTime
::currentDateTime().addDays(+1).toString("MMdd");QString path
= QString("C:\\H12\\%1").arg(datePath
);QString childDir
= ui
->lineEditHChilder12
->text().toUtf8();if(!childDir
.isEmpty()){path
= QString("C:\\H12\\%1\\%2").arg(datePath
).arg(childDir
);}QDir
dir(path
);if(!dir
.exists()){dir
.mkdir(path
);}QString filePathName
=NULL;if(!childDir
.isEmpty()){filePathName
= QString("C:\\H12\\%1\\%2\\%3___").arg(datePath
).arg(childDir
).arg(strPone
);filePathName
+=QDateTime
::currentDateTime().toString("zzzz");filePathName
+=".jpg";screen
->grabWindow(0).save(filePathName
,"jpg");ui
->lineEditH12
->setText("");}else{filePathName
= QString("C:\\H12\\%1\\%2___").arg(datePath
).arg(strPone
);filePathName
+=QDateTime
::currentDateTime().toString("zzzz");filePathName
+=".jpg";screen
->grabWindow(0).save(filePathName
,"jpg");ui
->lineEditH12
->setText("");}}
}void Widget
::on_pushButtonI12_clicked()
{QString strPone
= ui
->lineEditI12
->text().toUtf8();if(strPone
.isEmpty()){QMessageBox
::warning(this,"error","錯誤,必須輸入SN");return;}QString pathTime
= QDateTime
::currentDateTime().toString("yyyy-MM-dd HH:mm:ss");QString pathTime2
= QDateTime
::currentDateTime().toString("yyyy-MM-dd");QString paths
= QString("%1 20:00:00").arg(pathTime2
);QDateTime nowTime
= QDateTime
::fromString(pathTime
,"yyyy-MM-dd HH:mm:ss");QDateTime fixedTime
= QDateTime
::fromString(paths
,"yyyy-MM-dd HH:mm:ss");uint nowTime_s
= nowTime
.toTime_t();uint fixedTime_s
= fixedTime
.toTime_t();int c
= fixedTime_s
-nowTime_s
;QScreen
*screen
= QGuiApplication
::primaryScreen();if(c
>0){QString datePath
= QDateTime
::currentDateTime().toString("MMdd");QString path
= QString("C:\\I12\\%1").arg(datePath
);QString childDir
= ui
->lineEditIChilder12
->text().toUtf8();if(!childDir
.isEmpty()){path
= QString("C:\\I12\\%1\\%2").arg(datePath
).arg(childDir
);}QDir
dir(path
);if(!dir
.exists()){dir
.mkdir(path
);}QString filePathName
=NULL;if(!childDir
.isEmpty()){filePathName
= QString("C:\\I12\\%1\\%2\\%3___").arg(datePath
).arg(childDir
).arg(strPone
);filePathName
+=QDateTime
::currentDateTime().toString("zzzz");filePathName
+=".jpg";screen
->grabWindow(0).save(filePathName
,"jpg");ui
->lineEditI12
->setText("");}else{filePathName
= QString("C:\\I12\\%1\\%2___").arg(datePath
).arg(strPone
);filePathName
+=QDateTime
::currentDateTime().toString("zzzz");filePathName
+=".jpg";screen
->grabWindow(0).save(filePathName
,"jpg");ui
->lineEditI12
->setText("");}}if(c
<0){QString datePath
= QDateTime
::currentDateTime().addDays(+1).toString("MMdd");QString path
= QString("C:\\I12\\%1").arg(datePath
);QString childDir
= ui
->lineEditIChilder12
->text().toUtf8();if(!childDir
.isEmpty()){path
= QString("C:\\I12\\%1\\%2").arg(datePath
).arg(childDir
);}QDir
dir(path
);if(!dir
.exists()){dir
.mkdir(path
);}QString filePathName
=NULL;if(!childDir
.isEmpty()){filePathName
= QString("C:\\I12\\%1\\%2\\%3___").arg(datePath
).arg(childDir
).arg(strPone
);filePathName
+=QDateTime
::currentDateTime().toString("zzzz");filePathName
+=".jpg";screen
->grabWindow(0).save(filePathName
,"jpg");ui
->lineEditI12
->setText("");}else{filePathName
= QString("C:\\I12\\%1\\%2___").arg(datePath
).arg(strPone
);filePathName
+=QDateTime
::currentDateTime().toString("zzzz");filePathName
+=".jpg";screen
->grabWindow(0).save(filePathName
,"jpg");ui
->lineEditI12
->setText("");}}
}void Widget
::on_pushButtonI22_clicked()
{QString strPone
= ui
->lineEditI22
->text().toUtf8();if(strPone
.isEmpty()){QMessageBox
::warning(this,"error","錯誤,必須輸入SN");return;}QString pathTime
= QDateTime
::currentDateTime().toString("yyyy-MM-dd HH:mm:ss");QString pathTime2
= QDateTime
::currentDateTime().toString("yyyy-MM-dd");QString paths
= QString("%1 20:00:00").arg(pathTime2
);QDateTime nowTime
= QDateTime
::fromString(pathTime
,"yyyy-MM-dd HH:mm:ss");QDateTime fixedTime
= QDateTime
::fromString(paths
,"yyyy-MM-dd HH:mm:ss");uint nowTime_s
= nowTime
.toTime_t();uint fixedTime_s
= fixedTime
.toTime_t();int c
= fixedTime_s
-nowTime_s
;QScreen
*screen
= QGuiApplication
::primaryScreen();if(c
>0){QString datePath
= QDateTime
::currentDateTime().toString("MMdd");QString path
= QString("C:\\I22\\%1").arg(datePath
);QString childDir
= ui
->lineEditIChilder22
->text().toUtf8();if(!childDir
.isEmpty()){path
= QString("C:\\I22\\%1\\%2").arg(datePath
).arg(childDir
);}QDir
dir(path
);if(!dir
.exists()){dir
.mkdir(path
);}QString filePathName
=NULL;if(!childDir
.isEmpty()){filePathName
= QString("C:\\I22\\%1\\%2\\%3___").arg(datePath
).arg(childDir
).arg(strPone
);filePathName
+=QDateTime
::currentDateTime().toString("zzzz");filePathName
+=".jpg";screen
->grabWindow(0).save(filePathName
,"jpg");ui
->lineEditI22
->setText("");}else{filePathName
= QString("C:\\I22\\%1\\%2___").arg(datePath
).arg(strPone
);filePathName
+=QDateTime
::currentDateTime().toString("zzzz");filePathName
+=".jpg";screen
->grabWindow(0).save(filePathName
,"jpg");ui
->lineEditI22
->setText("");}}if(c
<0){QString datePath
= QDateTime
::currentDateTime().addDays(+1).toString("MMdd");QString path
= QString("C:\\I22\\%1").arg(datePath
);QString childDir
= ui
->lineEditIChilder22
->text().toUtf8();if(!childDir
.isEmpty()){path
= QString("C:\\I22\\%1\\%2").arg(datePath
).arg(childDir
);}QDir
dir(path
);if(!dir
.exists()){dir
.mkdir(path
);}QString filePathName
=NULL;if(!childDir
.isEmpty()){filePathName
= QString("C:\\I22\\%1\\%2\\%3___").arg(datePath
).arg(childDir
).arg(strPone
);filePathName
+=QDateTime
::currentDateTime().toString("zzzz");filePathName
+=".jpg";screen
->grabWindow(0).save(filePathName
,"jpg");ui
->lineEditI22
->setText("");}else{filePathName
= QString("C:\\I22\\%1\\%2___").arg(datePath
).arg(strPone
);filePathName
+=QDateTime
::currentDateTime().toString("zzzz");filePathName
+=".jpg";screen
->grabWindow(0).save(filePathName
,"jpg");ui
->lineEditI22
->setText("");}}
}void Widget
::on_pushButtonJ12_clicked()
{QString strPone
= ui
->lineEditJ12
->text().toUtf8();if(strPone
.isEmpty()){QMessageBox
::warning(this,"error","錯誤,必須輸入SN");return;}QString pathTime
= QDateTime
::currentDateTime().toString("yyyy-MM-dd HH:mm:ss");QString pathTime2
= QDateTime
::currentDateTime().toString("yyyy-MM-dd");QString paths
= QString("%1 20:00:00").arg(pathTime2
);QDateTime nowTime
= QDateTime
::fromString(pathTime
,"yyyy-MM-dd HH:mm:ss");QDateTime fixedTime
= QDateTime
::fromString(paths
,"yyyy-MM-dd HH:mm:ss");uint nowTime_s
= nowTime
.toTime_t();uint fixedTime_s
= fixedTime
.toTime_t();int c
= fixedTime_s
-nowTime_s
;QScreen
*screen
= QGuiApplication
::primaryScreen();if(c
>0){QString datePath
= QDateTime
::currentDateTime().toString("MMdd");QString path
= QString("C:\\J12\\%1").arg(datePath
);QString childDir
= ui
->lineEditJChilder12
->text().toUtf8();if(!childDir
.isEmpty()){path
= QString("C:\\J12\\%1\\%2").arg(datePath
).arg(childDir
);}QDir
dir(path
);if(!dir
.exists()){dir
.mkdir(path
);}QString filePathName
=NULL;if(!childDir
.isEmpty()){filePathName
= QString("C:\\J12\\%1\\%2\\%3___").arg(datePath
).arg(childDir
).arg(strPone
);filePathName
+=QDateTime
::currentDateTime().toString("zzzz");filePathName
+=".jpg";screen
->grabWindow(0).save(filePathName
,"jpg");ui
->lineEditJ12
->setText("");}else{filePathName
= QString("C:\\J12\\%1\\%2___").arg(datePath
).arg(strPone
);filePathName
+=QDateTime
::currentDateTime().toString("zzzz");filePathName
+=".jpg";screen
->grabWindow(0).save(filePathName
,"jpg");ui
->lineEditJ12
->setText("");}}if(c
<0){QString datePath
= QDateTime
::currentDateTime().addDays(+1).toString("MMdd");QString path
= QString("C:\\J12\\%1").arg(datePath
);QString childDir
= ui
->lineEditJChilder12
->text().toUtf8();if(!childDir
.isEmpty()){path
= QString("C:\\J12\\%1\\%2").arg(datePath
).arg(childDir
);}QDir
dir(path
);if(!dir
.exists()){dir
.mkdir(path
);}QString filePathName
=NULL;if(!childDir
.isEmpty()){filePathName
= QString("C:\\J12\\%1\\%2\\%3___").arg(datePath
).arg(childDir
).arg(strPone
);filePathName
+=QDateTime
::currentDateTime().toString("zzzz");filePathName
+=".jpg";screen
->grabWindow(0).save(filePathName
,"jpg");ui
->lineEditJ12
->setText("");}else{filePathName
= QString("C:\\J12\\%1\\%2___").arg(datePath
).arg(strPone
);filePathName
+=QDateTime
::currentDateTime().toString("zzzz");filePathName
+=".jpg";screen
->grabWindow(0).save(filePathName
,"jpg");ui
->lineEditJ12
->setText("");}}
}void Widget
::on_pushButtonK12_clicked()
{QString strPone
= ui
->lineEditK12
->text().toUtf8();if(strPone
.isEmpty()){QMessageBox
::warning(this,"error","錯誤,必須輸入SN");return;}QString pathTime
= QDateTime
::currentDateTime().toString("yyyy-MM-dd HH:mm:ss");QString pathTime2
= QDateTime
::currentDateTime().toString("yyyy-MM-dd");QString paths
= QString("%1 20:00:00").arg(pathTime2
);QDateTime nowTime
= QDateTime
::fromString(pathTime
,"yyyy-MM-dd HH:mm:ss");QDateTime fixedTime
= QDateTime
::fromString(paths
,"yyyy-MM-dd HH:mm:ss");uint nowTime_s
= nowTime
.toTime_t();uint fixedTime_s
= fixedTime
.toTime_t();int c
= fixedTime_s
-nowTime_s
;QScreen
*screen
= QGuiApplication
::primaryScreen();if(c
>0){QString datePath
= QDateTime
::currentDateTime().toString("MMdd");QString path
= QString("C:\\K12\\%1").arg(datePath
);QString childDir
= ui
->lineEditKChilder12
->text().toUtf8();if(!childDir
.isEmpty()){path
= QString("C:\\K12\\%1\\%2").arg(datePath
).arg(childDir
);}QDir
dir(path
);if(!dir
.exists()){dir
.mkdir(path
);}QString filePathName
=NULL;if(!childDir
.isEmpty()){filePathName
= QString("C:\\K12\\%1\\%2\\%3___").arg(datePath
).arg(childDir
).arg(strPone
);filePathName
+=QDateTime
::currentDateTime().toString("zzzz");filePathName
+=".jpg";screen
->grabWindow(0).save(filePathName
,"jpg");ui
->lineEditK12
->setText("");}else{filePathName
= QString("C:\\K12\\%1\\%2___").arg(datePath
).arg(strPone
);filePathName
+=QDateTime
::currentDateTime().toString("zzzz");filePathName
+=".jpg";screen
->grabWindow(0).save(filePathName
,"jpg");ui
->lineEditK12
->setText("");}}if(c
<0){QString datePath
= QDateTime
::currentDateTime().addDays(+1).toString("MMdd");QString path
= QString("C:\\K12\\%1").arg(datePath
);QString childDir
= ui
->lineEditKChilder12
->text().toUtf8();if(!childDir
.isEmpty()){path
= QString("C:\\K12\\%1\\%2").arg(datePath
).arg(childDir
);}QDir
dir(path
);if(!dir
.exists()){dir
.mkdir(path
);}QString filePathName
=NULL;if(!childDir
.isEmpty()){filePathName
= QString("C:\\K12\\%1\\%2\\%3___").arg(datePath
).arg(childDir
).arg(strPone
);filePathName
+=QDateTime
::currentDateTime().toString("zzzz");filePathName
+=".jpg";screen
->grabWindow(0).save(filePathName
,"jpg");ui
->lineEditK12
->setText("");}else{filePathName
= QString("C:\\K12\\%1\\%2___").arg(datePath
).arg(strPone
);filePathName
+=QDateTime
::currentDateTime().toString("zzzz");filePathName
+=".jpg";screen
->grabWindow(0).save(filePathName
,"jpg");ui
->lineEditK12
->setText("");}}
}void Widget
::on_pushButtonK22_clicked()
{QString strPone
= ui
->lineEditK22
->text().toUtf8();if(strPone
.isEmpty()){QMessageBox
::warning(this,"error","錯誤,必須輸入SN");return;}QString pathTime
= QDateTime
::currentDateTime().toString("yyyy-MM-dd HH:mm:ss");QString pathTime2
= QDateTime
::currentDateTime().toString("yyyy-MM-dd");QString paths
= QString("%1 20:00:00").arg(pathTime2
);QDateTime nowTime
= QDateTime
::fromString(pathTime
,"yyyy-MM-dd HH:mm:ss");QDateTime fixedTime
= QDateTime
::fromString(paths
,"yyyy-MM-dd HH:mm:ss");uint nowTime_s
= nowTime
.toTime_t();uint fixedTime_s
= fixedTime
.toTime_t();int c
= fixedTime_s
-nowTime_s
;QScreen
*screen
= QGuiApplication
::primaryScreen();if(c
>0){QString datePath
= QDateTime
::currentDateTime().toString("MMdd");QString path
= QString("C:\\K22\\%1").arg(datePath
);QString childDir
= ui
->lineEditKChilder22
->text().toUtf8();if(!childDir
.isEmpty()){path
= QString("C:\\K22\\%1\\%2").arg(datePath
).arg(childDir
);}QDir
dir(path
);if(!dir
.exists()){dir
.mkdir(path
);}QString filePathName
=NULL;if(!childDir
.isEmpty()){filePathName
= QString("C:\\K22\\%1\\%2\\%3___").arg(datePath
).arg(childDir
).arg(strPone
);filePathName
+=QDateTime
::currentDateTime().toString("zzzz");filePathName
+=".jpg";screen
->grabWindow(0).save(filePathName
,"jpg");ui
->lineEditK22
->setText("");}else{filePathName
= QString("C:\\K22\\%1\\%2___").arg(datePath
).arg(strPone
);filePathName
+=QDateTime
::currentDateTime().toString("zzzz");filePathName
+=".jpg";screen
->grabWindow(0).save(filePathName
,"jpg");ui
->lineEditK22
->setText("");}}if(c
<0){QString datePath
= QDateTime
::currentDateTime().addDays(+1).toString("MMdd");QString path
= QString("C:\\K22\\%1").arg(datePath
);QString childDir
= ui
->lineEditKChilder22
->text().toUtf8();if(!childDir
.isEmpty()){path
= QString("C:\\K22\\%1\\%2").arg(datePath
).arg(childDir
);}QDir
dir(path
);if(!dir
.exists()){dir
.mkdir(path
);}QString filePathName
=NULL;if(!childDir
.isEmpty()){filePathName
= QString("C:\\K22\\%1\\%2\\%3___").arg(datePath
).arg(childDir
).arg(strPone
);filePathName
+=QDateTime
::currentDateTime().toString("zzzz");filePathName
+=".jpg";screen
->grabWindow(0).save(filePathName
,"jpg");ui
->lineEditK22
->setText("");}else{filePathName
= QString("C:\\K22\\%1\\%2___").arg(datePath
).arg(strPone
);filePathName
+=QDateTime
::currentDateTime().toString("zzzz");filePathName
+=".jpg";screen
->grabWindow(0).save(filePathName
,"jpg");ui
->lineEditK22
->setText("");}}
}void Widget
::on_pushButtonL12_clicked()
{QString strPone
= ui
->lineEditL12
->text().toUtf8();if(strPone
.isEmpty()){QMessageBox
::warning(this,"error","錯誤,必須輸入SN");return;}QString pathTime
= QDateTime
::currentDateTime().toString("yyyy-MM-dd HH:mm:ss");QString pathTime2
= QDateTime
::currentDateTime().toString("yyyy-MM-dd");QString paths
= QString("%1 20:00:00").arg(pathTime2
);QDateTime nowTime
= QDateTime
::fromString(pathTime
,"yyyy-MM-dd HH:mm:ss");QDateTime fixedTime
= QDateTime
::fromString(paths
,"yyyy-MM-dd HH:mm:ss");uint nowTime_s
= nowTime
.toTime_t();uint fixedTime_s
= fixedTime
.toTime_t();int c
= fixedTime_s
-nowTime_s
;QScreen
*screen
= QGuiApplication
::primaryScreen();if(c
>0){QString datePath
= QDateTime
::currentDateTime().toString("MMdd");QString path
= QString("C:\\L12\\%1").arg(datePath
);QString childDir
= ui
->lineEditLChilder12
->text().toUtf8();if(!childDir
.isEmpty()){path
= QString("C:\\L12\\%1\\%2").arg(datePath
).arg(childDir
);}QDir
dir(path
);if(!dir
.exists()){dir
.mkdir(path
);}QString filePathName
=NULL;if(!childDir
.isEmpty()){filePathName
= QString("C:\\L12\\%1\\%2\\%3___").arg(datePath
).arg(childDir
).arg(strPone
);filePathName
+=QDateTime
::currentDateTime().toString("zzzz");filePathName
+=".jpg";screen
->grabWindow(0).save(filePathName
,"jpg");ui
->lineEditL12
->setText("");}else{filePathName
= QString("C:\\L12\\%1\\%2___").arg(datePath
).arg(strPone
);filePathName
+=QDateTime
::currentDateTime().toString("zzzz");filePathName
+=".jpg";screen
->grabWindow(0).save(filePathName
,"jpg");ui
->lineEditL12
->setText("");}}if(c
<0){QString datePath
= QDateTime
::currentDateTime().addDays(+1).toString("MMdd");QString path
= QString("C:\\L12\\%1").arg(datePath
);QString childDir
= ui
->lineEditLChilder12
->text().toUtf8();if(!childDir
.isEmpty()){path
= QString("C:\\L12\\%1\\%2").arg(datePath
).arg(childDir
);}QDir
dir(path
);if(!dir
.exists()){dir
.mkdir(path
);}QString filePathName
=NULL;if(!childDir
.isEmpty()){filePathName
= QString("C:\\L12\\%1\\%2\\%3___").arg(datePath
).arg(childDir
).arg(strPone
);filePathName
+=QDateTime
::currentDateTime().toString("zzzz");filePathName
+=".jpg";screen
->grabWindow(0).save(filePathName
,"jpg");ui
->lineEditL12
->setText("");}else{filePathName
= QString("C:\\L12\\%1\\%2___").arg(datePath
).arg(strPone
);filePathName
+=QDateTime
::currentDateTime().toString("zzzz");filePathName
+=".jpg";screen
->grabWindow(0).save(filePathName
,"jpg");ui
->lineEditL12
->setText("");}}
}void Widget
::on_pushButtonL22_clicked()
{QString strPone
= ui
->lineEditL22
->text().toUtf8();if(strPone
.isEmpty()){QMessageBox
::warning(this,"error","錯誤,必須輸入SN");return;}QString pathTime
= QDateTime
::currentDateTime().toString("yyyy-MM-dd HH:mm:ss");QString pathTime2
= QDateTime
::currentDateTime().toString("yyyy-MM-dd");QString paths
= QString("%1 20:00:00").arg(pathTime2
);QDateTime nowTime
= QDateTime
::fromString(pathTime
,"yyyy-MM-dd HH:mm:ss");QDateTime fixedTime
= QDateTime
::fromString(paths
,"yyyy-MM-dd HH:mm:ss");uint nowTime_s
= nowTime
.toTime_t();uint fixedTime_s
= fixedTime
.toTime_t();int c
= fixedTime_s
-nowTime_s
;QScreen
*screen
= QGuiApplication
::primaryScreen();if(c
>0){QString datePath
= QDateTime
::currentDateTime().toString("MMdd");QString path
= QString("C:\\L22\\%1").arg(datePath
);QString childDir
= ui
->lineEditLChilder22
->text().toUtf8();if(!childDir
.isEmpty()){path
= QString("C:\\L22\\%1\\%2").arg(datePath
).arg(childDir
);}QDir
dir(path
);if(!dir
.exists()){dir
.mkdir(path
);}QString filePathName
=NULL;if(!childDir
.isEmpty()){filePathName
= QString("C:\\L22\\%1\\%2\\%3___").arg(datePath
).arg(childDir
).arg(strPone
);filePathName
+=QDateTime
::currentDateTime().toString("zzzz");filePathName
+=".jpg";screen
->grabWindow(0).save(filePathName
,"jpg");ui
->lineEditL22
->setText("");}else{filePathName
= QString("C:\\L22\\%1\\%2___").arg(datePath
).arg(strPone
);filePathName
+=QDateTime
::currentDateTime().toString("zzzz");filePathName
+=".jpg";screen
->grabWindow(0).save(filePathName
,"jpg");ui
->lineEditL22
->setText("");}}if(c
<0){QString datePath
= QDateTime
::currentDateTime().addDays(+1).toString("MMdd");QString path
= QString("C:\\L22\\%1").arg(datePath
);QString childDir
= ui
->lineEditLChilder22
->text().toUtf8();if(!childDir
.isEmpty()){path
= QString("C:\\L22\\%1\\%2").arg(datePath
).arg(childDir
);}QDir
dir(path
);if(!dir
.exists()){dir
.mkdir(path
);}QString filePathName
=NULL;if(!childDir
.isEmpty()){filePathName
= QString("C:\\L22\\%1\\%2\\%3___").arg(datePath
).arg(childDir
).arg(strPone
);filePathName
+=QDateTime
::currentDateTime().toString("zzzz");filePathName
+=".jpg";screen
->grabWindow(0).save(filePathName
,"jpg");ui
->lineEditL22
->setText("");}else{filePathName
= QString("C:\\L22\\%1\\%2___").arg(datePath
).arg(strPone
);filePathName
+=QDateTime
::currentDateTime().toString("zzzz");filePathName
+=".jpg";screen
->grabWindow(0).save(filePathName
,"jpg");ui
->lineEditL22
->setText("");}}
}
widget.h 頭文件的包含的代碼是
#ifndef WIDGET_H
#define WIDGET_H#include <QWidget>namespace Ui
{
class Widget;
}class Widget : public QWidget
{Q_OBJECT
public:explicit Widget(QWidget
*parent
= 0);~Widget();private slots
:void on_tabWidget_tabBarClicked(int index
);void on_pushButtonP12_clicked();void on_pushButtonQ12_clicked();void on_pushButtonR12_clicked();void on_pushButtonH12_clicked();void on_pushButtonI12_clicked();void on_pushButtonI22_clicked();void on_pushButtonJ12_clicked();void on_pushButtonK12_clicked();void on_pushButtonK22_clicked();void on_pushButtonL12_clicked();void on_pushButtonL22_clicked();private:Ui
::Widget
*ui
;
};#endif
代碼寫的有的冗余,其實可以精簡代碼,把中間相同部份抽離出來一個公共函數,通過傳參數,直接調用函數,能夠節省到只有200行代碼左右,有興趣的朋友可以自己抽離出一個公共方法或者函數。頭文件先定義一下,cpp文件實現一下。
總結
以上是生活随笔為你收集整理的Qt实现截图的源码的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。