加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
controlspage.cpp 6.17 KB
一键复制 编辑 原始数据 按行查看 历史
聆听随风 提交于 2021-08-25 09:33 . 设置图片只适应Dlabel
#include "controlspage.h"
#include <QGridLayout>
#include <QProcess>
#include <DDialog>
#include "base64.h"
ControlsPage::ControlsPage(QWidget *parent)
: QWidget(parent),
m_lineEdit(new DLineEdit),
m_picPathEdit(new DFileChooserEdit),
m_edit(new DTextEdit),
m_labImage(new DLabel),
m_toast(new DToast(this)),
m_btnClear(new DPushButton("清空数据")),
m_btnBase64ToText(new DPushButton("base64转明文")),
m_btnBase64ToImage(new DPushButton("base64转图片"))
{
initUI();
initConnections();
}
void ControlsPage::showEvent(QShowEvent *event)
{
// 移动DToast的位置到窗口中下方的位置并显示
m_toast->move((width() - m_toast->width()) / 2.0,
height() - 10 - m_toast->height());
m_toast->pop();
QWidget::showEvent(event);
}
void ControlsPage::initUI()
{
// 统一设置
// 设置DToast的提示信息内容
m_toast->setText("Welcome To Use DtkEncrypt");
// 构建界面内容布局
QGridLayout *layout = new QGridLayout;
layout->setSpacing(10);
layout->addWidget(new DLabel("打开文件"), 0, 0, 1, 1, Qt::AlignRight);
layout->addWidget(new DLabel("输入明文"), 1, 0, 1, 1, Qt::AlignRight);
layout->addWidget(new DLabel("base64码"), 2, 0, 1, 1, Qt::AlignRight);
layout->addWidget(new DLabel("图片展示"), 3, 0, 1, 1, Qt::AlignRight);
m_picPathEdit->setFixedWidth(680);//340
m_picPathEdit->lineEdit()->setClearButtonEnabled(true);
m_picPathEdit->setText("~/Desktop/1.png");
m_picPathEdit->setNameFilters(QStringList() << "*.png *.bmp *.jpg *.jpeg");
layout->addWidget(m_picPathEdit, 0,2,1,1, Qt::AlignCenter);
m_lineEdit->setFixedWidth(680);
m_lineEdit->lineEdit()->setPlaceholderText("请输入文本");
m_lineEdit->lineEdit()->setClearButtonEnabled(true);
layout->addWidget(m_lineEdit, 1, 2, 1, 1, Qt::AlignLeft);
m_edit->setFixedSize(680, 150);
layout->addWidget(m_edit, 2, 2, 1, 1, Qt::AlignLeft);
QSizePolicy sizePolicy(QSizePolicy::Preferred, QSizePolicy::Expanding);
sizePolicy.setHorizontalStretch(0);
sizePolicy.setVerticalStretch(0);
sizePolicy.setHeightForWidth(m_labImage->sizePolicy().hasHeightForWidth());
m_labImage->setSizePolicy(sizePolicy);
m_labImage->setFrameShape(QFrame::Box);
m_labImage->setFrameShadow(QFrame::Sunken);
m_labImage->setFixedSize(680, 344);
m_labImage->setAlignment(Qt::AlignCenter);
layout->addWidget(m_labImage, 3, 2, 1, 1, Qt::AlignLeft);
//设置按钮大小
m_btnClear->setFixedSize(150, 36);
m_btnBase64ToText->setFixedSize(150, 36);
m_btnBase64ToImage->setFixedSize(150, 36);
layout->addWidget(m_btnBase64ToText, 2, 3, 1, 1, Qt::AlignLeft);
layout->addWidget(m_btnBase64ToImage, 2, 3, 2, 1, Qt::AlignLeft);
layout->addWidget(m_btnClear, 3, 3, 16, 1, Qt::AlignLeft);
// 竖直方向挤压内容,内容紧凑,而且会好看一点
QVBoxLayout *vbox = new QVBoxLayout;
vbox->addStretch();
vbox->addLayout(layout);
vbox->addStretch();
// 水平方向挤压内容,内容紧凑,而且会好看一点
QHBoxLayout *hbox = new QHBoxLayout;
hbox->addStretch();
hbox->addLayout(vbox);
hbox->addStretch();
setLayout(hbox);
}
void ControlsPage::initConnections()
{
// connect(m_passwordEdit, &DPasswordEdit::editingFinished, this, [this] {
// m_message->setText("Guess what, I know your password: \n" + m_passwordEdit->text());
// });
connect(m_picPathEdit, &DFileChooserEdit::fileChoosed, this, [=](const QString &filename) { pictureConvertBase64(filename); });
connect(m_lineEdit,&DLineEdit::textChanged, this, [=](const QString& text) {
if (!text.isEmpty())
textConvertBase64(text);
});
connect(m_btnClear, &DPushButton::clicked, this, [this] () {
m_picPathEdit->clear();
m_lineEdit->clear();
m_edit->clear();
m_labImage->clear();
});
connect(m_btnBase64ToText, &DPushButton::clicked, this, [this]() {
base64ConvertText();
});
connect(m_btnBase64ToImage, &DPushButton::clicked, this, [this]() {
base64ConvertImage();
});
}
void ControlsPage::pictureConvertBase64(const QString &filename)
{
if (!filename.isEmpty()) {
QFileInfo fileinfo = QFileInfo(filename);
//文件后缀
QString file_suffix = fileinfo.suffix();
m_edit->setText(Base64::imageToBase64(QImage(filename),file_suffix));
QPixmap pix(filename);
//设置为等比例缩放
pix.scaled(m_labImage->size() - QSize(4, 4), Qt::KeepAspectRatio);
//使pixmap自适应label大小
m_labImage->setScaledContents(true);
m_labImage->setPixmap(pix);
}
}
void ControlsPage::base64ConvertImage()
{
if(m_edit->toPlainText().isEmpty())
{
DDialog dialog("", "Base64编码框为空,请输入!", nullptr);
dialog.setIcon(DStyle().standardIcon(DStyle::SP_MessageBoxWarning));
dialog.addButton("确定");
dialog.exec();
return;
}
QString text = m_edit->toPlainText().trimmed();
if (!text.isEmpty()) {
QPixmap pix = QPixmap::fromImage(Base64::base64ToImage(text));
pix = pix.scaled(m_labImage->size() - QSize(4, 4), Qt::KeepAspectRatio);
m_labImage->setPixmap(pix);
}
}
/*
IgnoreAspectRatio 矩形框有多大,图片就缩放成多大,不限制原图片的长宽比
KeepAspectRatio 保持原图片的长宽比,且不超过矩形框的大小
KeepAspectRatioByExpanding 根据矩形框的大小最大缩放图片
*/
void ControlsPage::textConvertBase64(const QString &text)
{
m_edit->setText(Base64::textToBase64(text));
}
void ControlsPage::base64ConvertText()
{
if(m_edit->toPlainText().isEmpty())
{
DDialog dialog("", "Base64编码框为空,请输入!", nullptr);
dialog.setIcon(DStyle().standardIcon(DStyle::SP_MessageBoxWarning));
dialog.addButton("确定");
dialog.exec();
return;
}
QString text = m_edit->toPlainText().trimmed();
if (!text.isEmpty()) {
m_lineEdit->setText(Base64::base64ToText(text));
}
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化