#include "mainwindow.h"
#include <QPushButton>
#include <QWidget>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QLineEdit>
#include <QFileDialog>
#include <QMessageBox>
#include <poppler-qt5.h>
#include <QDebug>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, m_btn(new QPushButton("生成"))
, m_mainWdg(new QWidget(this))
, m_edtSelect(new QLineEdit())
, m_btnSelect(new QPushButton("选择视频路径"))
, m_layoutSelect(new QHBoxLayout())
, m_edtSave(new QLineEdit())
, m_btnSave(new QPushButton("图片保存路径"))
, m_layoutSave(new QHBoxLayout())
, m_layoutMain(new QVBoxLayout())
{
initUI();
initConnect();
}
MainWindow::~MainWindow()
{
}
void MainWindow::slotClickBtn()
{
QString filenamePath=m_edtSelect->text();
if(filenamePath.isEmpty()){
QMessageBox::information(this,"提示","未选择文件");
return;
}
if(m_edtSave->text().isEmpty()){
QMessageBox::information(this,"提示","未保存路径");
return;
}
QFileInfo fileinfo =QFileInfo(filenamePath);
QString fileName = fileinfo.fileName();
Poppler::Document* document = Poppler::Document::load(filenamePath);
uint now_time = QDateTime::currentDateTime().toTime_t();
for(int i = 0; i < document->numPages();i++){
Poppler::Page* pdfPage = document->page(i);
QImage image = pdfPage->renderToImage(600,600);
QString strSaveImageFileName = m_edtSave->text() + "/" + QString::number(now_time) + "_" + QString::number(i)+".png";
image.save(strSaveImageFileName);
}
QMessageBox::information(this,"提示","生成完毕");
}
void MainWindow::slotSelectPDF()
{
QString strFileName = QFileDialog::getOpenFileName();
m_edtSelect->setText(strFileName);
}
void MainWindow::slotSaveImg()
{
QString strPath = QFileDialog::getExistingDirectory();
m_edtSave->setText(strPath);
}
void MainWindow::initUI()
{
setMinimumWidth(500);
setWindowTitle("PDF转图片");
m_edtSelect->setDisabled(true);
m_layoutSelect->addWidget(m_edtSelect);
m_layoutSelect->addWidget(m_btnSelect);
m_layoutSave->addWidget(m_edtSave);
m_layoutSave->addWidget(m_btnSave);
m_layoutMain->addLayout(m_layoutSelect);
m_layoutMain->addLayout(m_layoutSave);
m_layoutMain->addWidget(m_btn);
m_mainWdg->setLayout(m_layoutMain);
setCentralWidget(m_mainWdg);
}
void MainWindow::initConnect()
{
connect(m_btn,&QPushButton::clicked,this,&MainWindow::slotClickBtn);
connect(m_btnSelect,&QPushButton::clicked,this,&MainWindow::slotSelectPDF);
connect(m_btnSave,&QPushButton::clicked,this,&MainWindow::slotSaveImg);
}