qt_demoe/tool/base64helper/frmbase64helper.cpp

98 lines
2.6 KiB
C++
Raw Normal View History

2021-02-20 01:26:02 +00:00
#pragma execution_character_set("utf-8")
2021-09-16 06:48:38 +00:00
#include "frmbase64helper.h"
#include "ui_frmbase64helper.h"
#include "base64helper.h"
2021-02-20 01:26:02 +00:00
#include "qfiledialog.h"
2021-07-31 07:02:53 +00:00
#include "qelapsedtimer.h"
2021-02-20 01:26:02 +00:00
#include "qdebug.h"
2021-09-16 06:48:38 +00:00
frmBase64Helper::frmBase64Helper(QWidget *parent) : QWidget(parent), ui(new Ui::frmBase64Helper)
2021-02-20 01:26:02 +00:00
{
ui->setupUi(this);
}
2021-09-16 06:48:38 +00:00
frmBase64Helper::~frmBase64Helper()
2021-02-20 01:26:02 +00:00
{
delete ui;
}
2021-09-16 06:48:38 +00:00
void frmBase64Helper::on_btnOpen_clicked()
2021-02-20 01:26:02 +00:00
{
QString fileName = QFileDialog::getOpenFileName(this, "选择文件", "", "图片(*.png *.bmp *.jpg)");
if (!fileName.isEmpty()) {
ui->txtFile->setText(fileName);
QPixmap pix(fileName);
pix = pix.scaled(ui->labImage->size() - QSize(4, 4), Qt::KeepAspectRatio);
ui->labImage->setPixmap(pix);
}
}
2021-09-16 06:48:38 +00:00
void frmBase64Helper::on_btnClear_clicked()
2021-02-20 01:26:02 +00:00
{
ui->txtFile->clear();
ui->txtText->clear();
ui->txtBase64->clear();
ui->labImage->clear();
}
2021-09-16 06:48:38 +00:00
void frmBase64Helper::on_btnImageToBase64_clicked()
2021-02-20 01:26:02 +00:00
{
2021-07-31 07:02:53 +00:00
//计时
QElapsedTimer time;
time.start();
2021-02-20 01:26:02 +00:00
QString fileName = ui->txtFile->text().trimmed();
if (!fileName.isEmpty()) {
2021-09-16 06:48:38 +00:00
ui->txtBase64->setText(Base64Helper::imageToBase64(QImage(fileName)));
2021-02-20 01:26:02 +00:00
}
2021-07-31 07:02:53 +00:00
//统计用时
#if (QT_VERSION >= QT_VERSION_CHECK(4,8,0))
double elapsed = (double)time.nsecsElapsed() / 1000000;
#else
double elapsed = (double)time.elapsed();
#endif
QString strTime = QString::number(elapsed, 'f', 3);
2021-11-26 12:21:49 +00:00
ui->labInfo->setText(QString("图片转base64 用时 %1 毫秒").arg(strTime));
2021-02-20 01:26:02 +00:00
}
2021-09-16 06:48:38 +00:00
void frmBase64Helper::on_btnBase64ToImage_clicked()
2021-02-20 01:26:02 +00:00
{
2021-07-31 07:02:53 +00:00
//计时
QElapsedTimer time;
time.start();
2021-02-20 01:26:02 +00:00
QString text = ui->txtBase64->toPlainText().trimmed();
if (!text.isEmpty()) {
2021-09-16 06:48:38 +00:00
QPixmap pix = QPixmap::fromImage(Base64Helper::base64ToImage(text));
2021-02-20 01:26:02 +00:00
pix = pix.scaled(ui->labImage->size() - QSize(4, 4), Qt::KeepAspectRatio);
ui->labImage->setPixmap(pix);
}
2021-07-31 07:02:53 +00:00
//统计用时
#if (QT_VERSION >= QT_VERSION_CHECK(4,8,0))
double elapsed = (double)time.nsecsElapsed() / 1000000;
#else
double elapsed = (double)time.elapsed();
#endif
QString strTime = QString::number(elapsed, 'f', 3);
2021-11-26 12:21:49 +00:00
ui->labInfo->setText(QString("base64转图片 用时 %1 毫秒").arg(strTime));
2021-02-20 01:26:02 +00:00
}
2021-09-16 06:48:38 +00:00
void frmBase64Helper::on_btnTextToBase64_clicked()
2021-02-20 01:26:02 +00:00
{
QString text = ui->txtText->text().trimmed();
if (!text.isEmpty()) {
2021-09-16 06:48:38 +00:00
ui->txtBase64->setText(Base64Helper::textToBase64(text));
2021-02-20 01:26:02 +00:00
}
}
2021-09-16 06:48:38 +00:00
void frmBase64Helper::on_btnBase64ToText_clicked()
2021-02-20 01:26:02 +00:00
{
QString text = ui->txtBase64->toPlainText().trimmed();
if (!text.isEmpty()) {
2021-09-16 06:48:38 +00:00
ui->txtText->setText(Base64Helper::base64ToText(text));
2021-02-20 01:26:02 +00:00
}
}